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));
Related
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.
I have 10 value like this ( I use new Date() to create it ) :
Wed Jun 14 2017 18:51:33
Wed Feb 7 2017 18:51:33
Wed Apr 10 2017 18:51:33
Wed Jun 10 2017 18:51:33
Wed Jun 1 2017 18:51:33
....
How can I get value of last 5 day
you can get past dates by decrementing date you get from new Date() function.
check below code.
var yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
you can do date formatting in yesterday too.
In Javascript,
add dates to one array, iterate them
var datesArray = []; //add dates to this array
var lastfivedays = [];
datesArray.forEach(function(checkdate){
var currentDate = new Date();
var timeDiff = currentDate.getTime() - checkdate.getTime();
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
if(diffDays == -5){
lastfivedays.push(checkdate)
}
});
lastfivedays array contains dates of last 5 day
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');
}
I have a javascript function that takes in a number X and a date, and returns a new Date that is X number of days away:
function addDays(theDate, numDaysToAdd) {
var newDate = new Date();
return new Date(newDate.setDate(theDate.getDate() + numDaysToAdd));
}
I pass it a day that is Sat Jul 02 2016 16:03:06 GMT-0700 (Pacific Daylight Time) and a number 7, but the result I got was Thu Jun 09 2016 16:05:32 GMT-0700 (Pacific Daylight Time). Why is it giving me the correct date but wrong month?
The problem is that newDate is always created from the current date (new Date()). In other words, if this function is executed in June it will produce a date in June, then try to set a the day of the month as a offset from the input date.
You need to construct newDate as a copy of theDate:
function addDays(theDate, numDaysToAdd) {
var newDate = new Date(theDate);
newDate.setDate(theDate.getDate() + numDaysToAdd);
return newDate;
}
var d = new Date('Sat Jul 02 2016 16:03:06 GMT-0700 (Pacific Daylight Time)');
console.log(addDays(d, 7).toString());
You can add number of milliseconds to given date and it will generate correct date.
getTime() returns milliseconds from epoch.
offset = numDaysToAdd * 24 * 60 * 60 * 1000;
24: Hours in a day
60: Minutes in an hour
60: seconds in a minute
1000: milliseconds in a second
Date constructor takes milliseconds from epoch
function addDays(theDate, numDaysToAdd) {
var start = theDate.getTime();
var offset = numDaysToAdd * 24 * 60 * 60 * 1000;
return new Date(start + offset);
}
var today = new Date();
console.log(today, addDays(today, 10));
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');