How to subtract 5 days from a defined date - Google App Script - javascript

I'm trying to write a script to subtract 5 days from a defined date but seems not working, here's my code:
var End_Day = sheet.getRange(i + 2, 20).getValue();
Logger.log(End_Day);
var End_day_2 = new Date();
End_day_2.setDate(End_Day.getDate()-5);
Logger.log(End_day_2);
and the result is not just - 5 days:
11:18:47 AM Info Sat Jun 04 00:00:00 GMT+08:00 2022
11:18:47 AM Info Fri Apr 29 11:18:47 GMT+08:00 2022
I am quite confused why the date move from Jun to Apr.
Thanks for having a look

Try:
var End_Day = sheet.getRange(i + 2, 20).getValue();
var End_day_2 = new Date(End_Day.getTime() - (5 * (1000 * 60 * 60 * 24)))
Logger.log(End_Day);
Logger.log(End_day_2);
Function:
const endDay = sheet.getRange(i + 2, 20).getValue()
const endDay2 = DateFromDaysAgo(endDay, 5)
...
function DateFromDaysAgo(startDate, number) {
if (typeof startDate === `string`) { startDate = new Date(startDate) }
return new Date(startDate.getTime() - (number * (1000 * 60 * 60 * 24)))
}

You should learn more about Date.prototype.setDate().It only changes the day of the month of a given Date instance.
As the code you posted, the day of the month of End_Day is 4, End_day_2.setDate(4 - 5) equals to End_day_2.setDate(-1) and the month of End_day_2 is April according to the console result, because there're 30 days in April, setDate(-1) means setDate(29), so you got Apr 29 at the end. That's how it goes.
One right way to do is substracting 5 days worth of milliseconds.
function addDays(date, days){
const DAY_IN_MILLISECONDS = 24 * 60 * 60000;
return new Date(date.getTime() + days * DAY_IN_MILLISECONDS);
}
console.log(addDays(new Date(), -5).toString()); // 5 days ago

I am quite confused why the date move from Jun to Apr.
It's because you're setting date on today(End_day_2) and not on your predefined date(End_day).
Change
End_day_2.setDate(End_Day.getDate()-5);
to
End_Day.setDate(End_Day.getDate()-5);
console.info(End_Day);

If what's coming from the sheet is a string, you will have to convert the date string into a date object.
The other thing is you have to work in milliseconds as #vanowm says:
606024*5 = 432000 * 1000 = 432000000
so skipping the sheet entirely:
x = new Date
> Fri May 27 2022 11:24:01 GMT-0400 (Eastern Daylight Time)
y = new Date(x - 432000000)
> Sun May 22 2022 11:24:01 GMT-0400 (Eastern Daylight Time)

This will do the trick. Works with any date and can subtract any number of days
const subtractDays = (fromDate, numDays) => {
if (!(fromDate instanceof Date)) throw 'The first argument must be a date';
return new Date(new Date().setDate(fromDate.getDate() - +numDays));
};

Weekago
function weekago() {
let dt = new Date();
dt.setDate(dt.getDate()-7);
Logger.log(dt);
return dt;
}
Five days ago
function fiveago() {
let dt = new Date();
dt.setDate(dt.getDate()-5)
Logger.log(dt);
return dt;
}
Five days from a date in a spreadsheet cell
function fivefromadateinspreadsheet() {
const v = SpreadsheetApp.getActiveSheet().getRange("A1").getValue();
let dt = new Date(v);
dt.setDate(dt.getDate()-5);//Note that does not return a date it return the numbrer of milliseconds
Logger.log(dt);
return dt;
}

You can subtract 5 days from a defined date in Google App Script by using the Utilities.formatDate() method. Here's an example:
function subtractDays() {
var date = new Date();
var subtractDays = 5;
// Subtract 5 days from the current date
date.setDate(date.getDate() - subtractDays);
// Format the new date
var newDate = Utilities.formatDate(date, "UTC", "yyyy-MM-dd");
Logger.log(newDate);
}
In this example, we first create a Date object to represent the current date. Then, we subtract 5 days from the current date by using the setDate() method. Finally, we format the new date using the Utilities.formatDate() method and log it to the console using the Logger.log() method.
You can modify the subtractDays variable to subtract a different number of days from the date, or you can use a different date object to start with.

Related

How to calculate and check for a bi-weekly date

I really need your help,
Let's say my demarcation start date is: December 19, 2016 as defined by the variable x
How can I write a JavaScript function, such that it will check the present date against x and the present date against what the recurrence date will be (14) days from x as defined by the variable y.
var y = recurrence is every 14 days, thereafter from the date (x) with no end date specified (unlimited)
Ex.
function() {
if (present date == x) { alert(true) }
if (present date == y) { alert(true) }
}
You could get the number of days difference between your start date and the current date then check if that number is a multiple of 14.
function treatAsUTC(date) {
var result = new Date(date);
result.setMinutes(result.getMinutes() - result.getTimezoneOffset());
return result;
}
function daysBetween(startDate, endDate) {
var millisecondsPerDay = 24 * 60 * 60 * 1000;
return Math.floor((treatAsUTC(endDate) - treatAsUTC(startDate)) / millisecondsPerDay);
}
var demarcationdate = new Date("2016-12-19"),
today = new Date(),
days = daysBetween(demarcationdate,today),
daystill = 14 - days%14,
rec = days%14==0,
d = new Date();
d.setDate(today.getDate() + daystill);
var nextDate = (d.getDate() + "/" + (d.getMonth() + 1) + "/" + d.getFullYear());
console.log("Days diff = "+days+". Recurs today = "+rec+". Next in "+daystill+" days ("+nextDate.toString()+").");
jsFiddle
If Date.now() == 1482181410856, 14 days from now will be 1482181410856 + (14 * 24 * 60 * 60 * 1000) == 1483391010856.
let y = new Date(Date.now() + (14 * 24 * 60 * 60 * 1000));
console.log(y.toUTCString()); // "Mon, 02 Jan 2017 21:03:30 GMT"
Assuming you really want to compare precise dates, i.e. to the milliseconds, then:
var present_date = new Date();
if(present_date.getTime() === x.getTime()) alert("Today is the same date as x");
else {
var y = new Date(x.getTime());
y.setDate(y.getDate() + 14); // add 14 days
if(present_date.getTime() === y.getTime()) alert("Today is the same date as y");
}
But most of the time we want to compare dates as full days, not milliseconds, so you'd have to compare ranges instead (from midnight to 11:59PM)... In that case, I recommend using a library to make your life easier - like moment.js for instance...
Hope this helps!
This is probably a duplicate of Add +1 to current date.
If you have a start date, say 20 December, 2016, you can calculate 14 days after that by simply adding 14 days to the date. You can then check if today's date is either of those dates, e.g.
// Create a Date for 20 December, 2016 with time 00:00:00
var startDate = new Date(2016,11,20);
// Create a Date for the start + 14 days with time 00:00:00
var startPlus14 = new Date(startDate);
startPlus14.setDate(startPlus14.getDate() + 14);
// Get today and set the time to 00:00:00.000
var today = new Date();
today.setHours(0,0,0,0);
if (+today == +startDate) {
console.log('Today is the start date');
} else if (+today == +startPlus14) {
console.log('Today is 14 days after the start date');
} else {
console.log('Today is neither the start nor 14 days after the start');
}

How to get total number of hours between two dates in javascript?

I am in a situation where I need to find out the total hour difference between two date objects but the thing is dates aren't present in the actual format.
Date 1: 6 Apr, 2015 14:45
Date 2: 7 May, 2015 02:45
If it would have been in standard format, simply I would have been used below method:
var hours = Math.abs(date1 - date2) / 36e5;
I am not sure how do I get the hour difference here... please help.
You can create date objects out of your strings:
const dateOne = "6 Apr, 2015 14:45";
const dateTwo = "7 May, 2015 02:45";
const dateOneObj = new Date(dateOne);
const dateTwoObj = new Date(dateTwo);
const milliseconds = Math.abs(dateTwoObj - dateOneObj);
const hours = milliseconds / 36e5;
console.log(hours);
You can create two date objects from the strings you have provided
var date1 = new Date("6 Apr, 2015 14:45");
var date2 = new Date("7 May, 2015 02:45");
then you can simply get the timestamp of the two dates and find the difference
var difference = Math.abs(date1.getTime() - date2.getTime());
Now to convert that to hours simply convert it first to seconds (by dividing by 1000 because the result is in milliseconds), then divid it by 3600 (to convert it from seconds to hours)
var hourDifference = difference / 1000 / 3600;
var date1 = new Date("6 Apr, 2015 14:45").getTime() / 1000;
var date2 = new Date("7 May, 2015 02:45").getTime() / 1000;
var difference = (date2 - date1)/60/60;
console.log(difference); //732 hours or 30.5 days
I'm not sure what you mean, but this works for me.
<!DOCTYPE html>
<html>
<body>
<p id="hours"></p>
<script>
var d1 = new Date("1 May, 2015 14:45");
var d2 = new Date("29 April, 2015 14:45");
var hours = (d1-d2)/36e5;
document.getElementById("hours").innerHTML = hours;
</script>
</body>
</html>

Calculate the difference between 2 SQLite datetimes in Javascript

I'm have a startDate and a endDate stored in a SQLite database and need to calculate the difference in minutes and seconds between the 2 datetimes using javascript.
For example:
startDate = 2012-10-07 11:01:13
endDate = 2012-10-07 12:42:13
I've had a good read though loads of similar questions on SO but could only find things relating to calculating this as part of a select.
Convert the strings to a JS Date Object, subtract the dates, and use some aritmethic to calculate hours/seconds from the result. Something like:
function convertMS(ms) {
var d, h, m, s, ts, tm, th;
s = ts = Math.floor(ms / 1000);
m = tm = Math.floor(s / 60);
s = s % 60;
h = th = Math.floor(m / 60);
m = m % 60;
d = Math.floor(h / 24);
h = h % 24;
return { d: d, h: h, m: m, s: s, tm: tm, th: th, ts: ts};
};
var start = new Date('2012-10-07 11:01:13'.split('-').join('/'))
,end = new Date('2012-10-07 12:42:13'.split('-').join('/'))
,dif = convertMS(end - start);
console.log(dif.h+':'+dif.m);​​​​​​ //=> ​1:41
console.log('total minutes dif: '+dif.tm);​​​​​​ //=> total ​minutes dif: 101
console.log('total seconds dif: '+dif.ts);​​​​​​ //=> ​total seconds dif: 6060
[edit based on comment]
'Manual' parsing of a date string in the provided format:
Date.tryParse = function(ds){
var arr = ds.match(/\d+/g)
,err = 'Expected at least yyyy'
,dat = arr.length && String(arr[0]).length === 4
? doParse.apply(null,arr) : err;
return dat;
function doParse(y,m,d,h,mi,s,ms){
var dat = new Date(+y,(+m-1)||0,+d||1,+h||0,+mi||0,+s||0,+ms||0);
return isNaN(dat) ? new Date : dat;
}
}
var start = Date.tryParse('2012-10-07 11:01:13'); //=> Sun Oct 07 2012 11:01:13
var test = Date.tryParse('2009'); //=> Sun Jan 01 2009 00:00:00
All the answers are generally correct with respect to converting the dates to milliseconds in epoch time, subtracting those, and converting the result back from milliseconds into your required units (although the specific implementations offered elsewhere do not currently give you exactly what you asked for -- i.e., just the number of minutes and seconds between your two datetimes).
However, please also note...
new Date('2012-10-07 12:42:13')
... that this is not a reliable way to construct the Date from your SQLite date strings.
When you feed a string into the constructor of a Date object, you are effectively calling Date.parse(). That behaves differently on different browsers.
Check this out:
> new Date('1-1-2012');
Sun Jan 01 2012 00:00:00 GMT-0800 (PST)
> new Date('01-01-2012');
Sun Jan 01 2012 00:00:00 GMT-0800 (PST)
> new Date('2012-1-1');
Sun Jan 01 2012 00:00:00 GMT-0800 (PST)
Looks pretty good, right? But that's on Chrome.
Now check out what happens in an up-to-date version of Firefox, with the exact same calls:
> new Date('1-1-2012');
Date {Invalid Date}
> new Date('01-01-2012');
Date {Invalid Date}
> new Date('2012-1-1');
Date {Invalid Date}
> new Date('2012-01-01');
Date {Sat Dec 31 2011 16:00:00 GMT-0800 (PST)}
Furthermore, look at this behavior, in both browsers:
> new Date('2012-01-01');
Sat Dec 31 2011 16:00:00 GMT-0800 (PST)
Simply prepending zeroes to the month and date digits causes a time warp! You have to set the time and a timezone (for me, PST) to make that go away:
> new Date('2012-01-01T00:00:00-08:00')
Sun Jan 01 2012 00:00:00 GMT-0800 (PST)
Basically, dealing with date string parsing is a headache. You don't want to have to digest and account for specs like this, this, and this.
So, here's a better alternative -- pass the parts of your datetime as separate args to the constructor of the Date object. That will reliably create the date for you, so your subsequent comparisons are valid.
new Date(year, month, day [, hour, minute, second, millisecond])
Here's what that initialization could look like for your case:
// Extract month, day, year from SQLite format, 'trimming' whitespace.
var sqlLiteSampleStr = "2012-10-07 11:01:13";
var re = /^\s*(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})\s*$/;
var match = re.exec(sqlLiteSampleStr);
if (match) {
var year = parseInt(match[1]);
var month = parseInt(match[2]) - 1; // Zero-indexed months.
var date = parseInt(match[3]);
var hour = parseInt(match[4]);
var minute = parseInt(match[5]);
var second = parseInt(match[6]);
var date = new Date(year, month, date, hour, minute, second);
}
Note: be careful of timezone considerations. You don't seem to have any timezone data in that SQLite format snippet.
Update
#james-j clarified that he's looking for minutes and seconds specifically.
Here's a snippet to extract just minutes and seconds:
var msPerMin = 1000 * 60;
var min = Math.floor(timeInMs / msPerMin);
var timeInMs = timeInMs - (min * msPerMin);
var sec = Math.floor(timeInMs / 1000 );
Other answers are correct, however browsers do not consistently parse date strings (see below) so you should manually parse the strings. The format in the OP is not consistent with the format in ES5 either, and will not be correctly parsed by some browsers. A simple function to convert the OP format to a date that will work in all browsers is:
function stringToDate(s) {
s = s.split(/[-\/\. :]/);
return new Date(s[0], --s[1], s[2], s[3], s[4], s[5], 0)
}
There are some formats that are parsed by all browsers, however they are not standards compliant and if used, are dependent on all implementations continuing to support unspecified formats.
Edit
To tolerate malformed input (extra whitespace, different seperators, extra characters), match can be used instead of split:
function stringToDate(s) {
s = s.match(/\d+/g);
return new Date(s[0], --s[1], s[2], s[3], s[4], s[5], 0)
}
However, since the values are coming from a database and a format has been specified, the date strings should fit the format. Erroneous data should not be stored in the first place.
For completeness, here's a function to do what you want:
/* Return difference between two dates as minutes and seconds mm:ss.
* Input format is yyyy-mm-dd hh:mm:ss
* If strings don't converted to dates (incorrect format or invalid values),
* return undefined.
*/
function diffInMins(s0, s1) {
var diff;
// Convert strings to date ojbects
var d0 = stringToDate(s0);
var d1 = stringToDate(s1);
// Helper function to padd with leading zero
function z(n) {
return (n<10? '0' : '') + n;
}
// Check conversions
if (d0 && d1 && typeof d0 == 'object' && typeof d1 == 'object') {
// Return difference formatted as mm:ss
if (d0 && d1) {
diff = d1 - d0;
return z(diff/60000 | 0) + ':' + z(Math.round(diff%60000/1000));
}
}
}
JavaScript actually makes subtracting dates quite simple. Here's how it works:
// Create Date objects from the strings
startDate = new Date('2012-10-07 11:01:13');
endDate = new Date('2012-10-07 12:42:13');
// Subtract Date objects to return milliseconds between them
millisecondsBetween = (endDate - startDate);
// Convert to whatever you like
alert(millisecondsBetween/1000/60 + ' minutes');

javascript date comparison

I have lead_creat_date and I need to compare it against 20 days before date ( let's say today is aug-4-2011, than I need july-14-2011). So comparison against lead_creat_date and July-14-2011.
if ( lead_creat_date > july-14-2011)
{
alert('lead_creat_date is greater');
}
How can I do this comparison in JavaScript?
I'm trying using the JavaScript date object. I did get one number for 20 days before date, using setDate() & getDate() function but I don't know how to convert lead_creat_date into a JavaScript date() object.
Thanks.
It's likely you can use the Date.parse() method.
It really depends on your date format.
I'll assume lead_creat_date is a Date object, seeing as it's not clear...
It depends on how accurate you need to be. You can do something like this, which will go back exactly 20 days, to the millisecond.
var now = new Date().getTime() - 20 * 24 * 60 * 60 * 1000;
if (lead_creat_date > now) {
alert('lead_creat_date is greater');
}
If you only care about the day, you could probably do this
var now = new Date().getTime() - 20 * 24 * 60 * 60 * 1000;
now = new Date(now.toDateString());
if (lead_creat_date > now) {
alert('lead_creat_date is greater');
}
References:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/toDateString
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTime
To convert a string to a date, convert the parts to numbers to use as input to the Date constructor. e.g. if your date is July-14-2011 then you can convert it using:
var dateString = 'July-14-2011';
var months = {january:0, february:1, march:2, april:3,
may:4, june:5, july:6, august:7, september:8,
october:9, november:10, december:11};
var dateBits = dateString.split('-');
var monthNumber = months[dateBits[0].toLowerCase()];
// Date object for date string
var date = new Date(dateBits[2], monthNumber, dateBits[1]);
// 20 days prior
date.setDate(date.getDate() - 20); // 24 Jun 2011
Edit
If your date format is 8/27/2009 10:23:00 AM the you can convert to a date using:
var dateString = '8/3/2011 10:23:00 AM';
var dateBits = dateString.split(/[ \/]/);
var date = new Date(dateBits[2], dateBits[0] - 1, dateBits[1]);
// 20 days prior
date.setDate(date.getDate() - 20); // 14 Jul 2011
alert(date);
If you need to include the time, you can include it using the same strategy, e.g.
var dateString = '8/3/2011 10:23:00 AM';
var dateBits = dateString.split(/[ \/:]/);
if (dateBits[6].toLowerCase() == 'pm') {
dateBits[3] = +dateBits[3] + 12;
}
// Thu 14 Jul 2011 10:23:00
var date = new Date(dateBits[2], dateBits[0] - 1, dateBits[1] - 20,
dateBits[3], dateBits[4], dateBits[5]);
and as a function:
function offsetDate(dateString, offset) {
var dateBits = dateString.split(/[ \/:]/);
if (dateBits[6].toLowerCase() == 'pm') {
dateBits[3] = +dateBits[3] + 12;
}
return new Date(dateBits[2], dateBits[0] - 1,
+dateBits[1] + +offset,
dateBits[3], dateBits[4], dateBits[5]);
}
// Thu 14 Jul 2011 10:23:00
alert(offsetDate('8/3/2011 10:23:00 AM', -20));
// Tue 23 Aug 2011 22:23:00
alert(offsetDate('8/3/2011 10:23:00 PM', +20));
// Wed 18 Jan 2012 10:23:00
alert(offsetDate('12/29/2011 10:23:00 AM', +20));

how can I convert day of year to date in javascript?

I want to take a day of the year and convert to an actual date using the Date object. Example: day 257 of 1929, how can I go about doing this?
"I want to take a day of the year and convert to an actual date using the Date object."
After re-reading your question, it sounds like you have a year number, and an arbitrary day number (e.g. a number within 0..365 (or 366 for a leap year)), and you want to get a date from that.
For example:
dateFromDay(2010, 301); // "Thu Oct 28 2010", today ;)
dateFromDay(2010, 365); // "Fri Dec 31 2010"
If it's that, can be done easily:
function dateFromDay(year, day){
var date = new Date(year, 0); // initialize a date in `year-01-01`
return new Date(date.setDate(day)); // add the number of days
}
You could add also some validation, to ensure that the day number is withing the range of days in the year supplied.
The shortest possible way is to create a new date object with the given year, January as month and your day of the year as date:
const date = new Date(2017, 0, 365);
console.log(date.toLocaleDateString());
As for setDate the correct month gets calculated if the given date is larger than the month's length.
// You might need both parts of it-
Date.fromDayofYear= function(n, y){
if(!y) y= new Date().getFullYear();
var d= new Date(y, 0, 1);
return new Date(d.setMonth(0, n));
}
Date.prototype.dayofYear= function(){
var d= new Date(this.getFullYear(), 0, 0);
return Math.floor((this-d)/8.64e+7);
}
var d=new Date().dayofYear();
//
alert('day#'+d+' is '+Date.fromDayofYear(d).toLocaleDateString())
/* returned value: (String)
day#301 is Thursday, October 28, 2010
*/
Here is a function that takes a day number, and returns the date object
optionally, it takes a year in YYYY format for parameter 2. If you leave it off, it will default to current year.
var getDateFromDayNum = function(dayNum, year){
var date = new Date();
if(year){
date.setFullYear(year);
}
date.setMonth(0);
date.setDate(0);
var timeOfFirst = date.getTime(); // this is the time in milliseconds of 1/1/YYYY
var dayMilli = 1000 * 60 * 60 * 24;
var dayNumMilli = dayNum * dayMilli;
date.setTime(timeOfFirst + dayNumMilli);
return date;
}
OUTPUT
// OUTPUT OF DAY 232 of year 1995
var pastDate = getDateFromDayNum(232,1995)
console.log("PAST DATE: " , pastDate);
PAST DATE: Sun Aug 20 1995 09:47:18 GMT-0400 (EDT)
Here's my implementation, which supports fractional days. The concept is simple: get the unix timestamp of midnight on the first day of the year, then multiply the desired day by the number of milliseconds in a day.
/**
* Converts day of the year to a unix timestamp
* #param {Number} dayOfYear 1-365, with support for floats
* #param {Number} year (optional) 2 or 4 digit year representation. Defaults to
* current year.
* #return {Number} Unix timestamp (ms precision)
*/
function dayOfYearToTimestamp(dayOfYear, year) {
year = year || (new Date()).getFullYear();
var dayMS = 1000 * 60 * 60 * 24;
// Note the Z, forcing this to UTC time. Without this it would be a local time, which would have to be further adjusted to account for timezone.
var yearStart = new Date('1/1/' + year + ' 0:0:0 Z');
return yearStart + ((dayOfYear - 1) * dayMS);
}
// usage
// 2015-01-01T00:00:00.000Z
console.log(new Date(dayOfYearToTimestamp(1, 2015)));
// support for fractional day (for satellite TLE propagation, etc)
// 2015-06-29T12:19:03.437Z
console.log(new Date(dayOfYearToTimestamp(180.51323423, 2015)).toISOString);
If I understand your question correctly, you can do that from the Date constructor like this
new Date(year, month, day, hours, minutes, seconds, milliseconds)
All arguments as integers
You have a few options;
If you're using a standard format, you can do something like:
new Date(dateStr);
If you'd rather be safe about it, you could do:
var date, timestamp;
try {
timestamp = Date.parse(dateStr);
} catch(e) {}
if(timestamp)
date = new Date(timestamp);
or simply,
new Date(Date.parse(dateStr));
Or, if you have an arbitrary format, split the string/parse it into units, and do:
new Date(year, month - 1, day)
Example of the last:
var dateStr = '28/10/2010'; // uncommon US short date
var dateArr = dateStr.split('/');
var dateObj = new Date(dateArr[2], parseInt(dateArr[1]) - 1, dateArr[0]);
this also works ..
function to2(x) { return ("0"+x).slice(-2); }
function formatDate(d){
return d.getFullYear()+"-"+to2(d.getMonth()+1)+"-"+to2(d.getDate());
}
document.write(formatDate(new Date(2016,0,257)));
prints "2016-09-13"
which is correct as 2016 is a leaap year. (see calendars here: http://disc.sci.gsfc.nasa.gov/julian_calendar.html )
If you always want a UTC date:
function getDateFromDayOfYear (year, day) {
return new Date(Date.UTC(year, 0, day))
}
console.log(getDateFromDayOfYear(2020, 1)) // 2020-01-01T00:00:00.000Z
console.log(getDateFromDayOfYear(2020, 305)) // 2020-10-31T00:00:00.000Z
console.log(getDateFromDayOfYear(2020, 366)) // 2020-12-31T00:00:00.000Z

Categories