Comparing dateTimes in JavaScript is not working [duplicate] - javascript

This question already has answers here:
Greater than operator given wrong response inside console.log in javascript
(2 answers)
Parsing a string to a date in JavaScript
(35 answers)
Closed 2 years ago.
I have 3 dateTimes in the same format. These 3 dateTimes have the same date at different times.
var orgstartdatetime = 'Mon Jan 01 1900 10:00:00 GMT+0519 (India Standard Time)';
var orgenddatetime = 'Mon Jan 01 1900 12:00:00 GMT+0519 (India Standard Time)';
var newdatetime = 'Mon Jan 01 1900 10:30:00 GMT+0519 (India Standard Time)';
I want to compare newdatetime is in between the orgstartdatetime and orgenddatetime.
I tried it like these 3 ways. But those are not working.
if (orgstartdatetime.getTime() <= newdatetime.getTime() < orgenddatetime.getTime() ) {
console.log('conflit');
}
if (orgstartdatetime.valueOf() <= newdatetime.valueOf() < orgenddatetime.valueOf() ) {
console.log('conflit');
}
if (+orgstartdatetime <= +newdatetime < +orgenddatetime ) {
console.log('conflit');
}
Please help me to fulfill this if condition.
Thank you

You first need to create a Date object to access getTime() method, try this:
var orgstartdatetime = new Date('Mon Jan 01 1900 10:00:00 GMT+0519 (India Standard Time)');

Related

converting time values into readable format and possibly in local time [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 5 years ago.
I am getting the values for sportsEndTime and sportsValueTime in readable format but where as other consoles I am not getting in readable format.
Values like 1513752960000 and 1513752960000 are not in readable format. How to convert into readable format in js. Since without readable format Its hard to underrstand the code.
var preEvent = {end: 1513752960000};
var selectedEvent = {start: 1513752960000};
let sportsEndTime = new Date(preEvent.end);
console.log("sportsEndTime--->" + sportsEndTime);
//sportsEndTime--->Wed Dec 20 2017 01:56:00 GMT-0500 (Eastern Standard Time)
let sportsValueTime = new Date(selectedEvent.start);
//sportsValueTime--->Wed Dec 20 2017 01:56:00 GMT-0500 (Eastern Standard Time)
console.log("sportsValueTime--->" + sportsValueTime);
console.log("sportsEndTime.getTime()--->" + sportsEndTime.getTime());
//sportsEndTime.getTime()--->1513752960000
console.log("sportsValueTime.getTime()--->" + sportsValueTime.getTime());
//sportsValueTime.getTime()--->1513752960000
console.log("sportsValueTime.getTime()--->" + (sportsValueTime.getTime() - 30000));
//sportsValueTime.getTime()--->1513752930000
console.log("sportsValueTime.setTime(sportsValueTime.getTime() - 30000)--->" + sportsValueTime.setTime(sportsValueTime.getTime() - 30000));
//sportsValueTime.setTime(sportsValueTime.getTime() - 30000)--->1513752930000
That's unreadable format called unix timestamps,
var timeStamp = Math.floor(Date.now() / 1000);
// 1513827258
var nDate = new Date(timeStamp * 1000);
// Thu Dec 21 2017 11:34:18 GMT+0800 (W. Australia Standard Time)
using moment.js would make it easier to manage date/time operation.

If statement with date comparison

I'm trying to write an if statement that runs some code if the date is after April 24th, 2017, 10 am EDT, but it doesn't appear that my variables are comparable (different data types?).
I'm trying to avoid using Moment.js for just this.
var today = new Date();
var launch = 'Mon Apr 24 2017 10:00:00 GMT-0400 (EDT)';
today returns Tue Apr 04 2017 14:34:41 GMT-0400 (EDT).
When I test if either is greater than the other, both are false. How should I format my dates?
Thanks!
You have to have launch as a date type:
var today = new Date();
var launch = 'Mon Apr 24 2017 10:00:00 GMT-0400 (EDT)';
var launchDate = Date.parse(launch);
if ( launchDate > today )
You should also read more about dates here:
Compare two dates with JavaScript

JavaScript: Comparing two dates no output

I would like to compare the given date in the below format in JaveScript. I have tried the following,
Thu May 19 2016 00:00:00 GMT+0530 (India Standard Time)
Thu May 20 2016 00:00:00 GMT+0530 (India Standard Time)
var ExpiryDate = userAccount.ExpiryDate();
var datetoday = new Date();
var Expired = (DateTime.Compare(ExpiryDate, datetoday) == -1 ) ? true : false;
//if expiry date is less than today date then var expired should be true
But didn't worked. I could not compare those two dates. It results in un handled exception. Is there any other way to do this date comparison in JaveScript ?
I have referred the following answers in SO but they are in different date format. So that I have raised this question,
javascript compare two dates and throw an alert
Javascript comparing two dates has wrong result
Compare two dates in JavaScript
Javascript compare two dates to get a difference
Any suggestion would be helpful.
var date = new Date();
//# => Fri May 20 2016 16:09:43 GMT+0530 (India Standard Time)
var date2 = new Date();
date2.setDate(date.getDate() - 1);
//# => Thu May 19 2016 16:09:43 GMT+0530 (India Standard Time)
date > date2 //# => true
use getTime()
var date1 = (new Date("20 May 2016")).getTime();
var date2 = (new Date("19 May 2016")).getTime();
date1>date2
You will find some good method here

Compare dates issue - javascript

I need to compare dates in javascript.
After attempt many ways...
I choose:
var endDate = new Date(secondDate.getYear(), secondDate.getMonth(), secondDate.getDate(), 0, 0, 0,0);
var startDate = new Date(firstDate.getYear(), firstDate.getMonth(), firstDate.getDate(), 0, 0, 0, 0);
if (endDate.getTime() >= startDate.getTime()) {
isValid = true;
}
else {
isValid = false;
}
In my situation:
---startDate = Tue Apr 01 1997 00:00:00 GMT+0200 (Jerusalem Standard Time) (i.e, 01/04/1997)
---endDate = Thu Jul 26 114 00:00:00 GMT+0200 (Jerusalem Standard Time) (i.e, 26/07/2014)
You see? startDate is small then endDate, right?
But:
---endDate.getTime() returns: -58551904800000
---startTime.getTime() returns: 859845600000
so, endDate.getTime() >= startDate.getTime() returns false...
In other situation, it works well:
---startDate: Sat Jul 21 114 00:00:00 GMT+0200 (Jerusalem Standard Time) (i.e, 21/07/2014)
---endDate: Sat Jul 28 114 00:00:00 GMT+0200 (Jerusalem Standard Time) (i.e, 28/07/2014)
---startDate.getTime() returns -58552336800000
---endDate.getTime() returns -58551732000000
so, endDate.getTime() >= startDate.getTime() returns true...
It seems like that javascript functions have another behavior for dates after year 2000.
What should I do? which code will be match to all of the optional situations?
Thanks.
Yeah like ghusse said, there is a problem with your end time if you fixed it so it was 2014 you would get a result such as 1406329200000 instead of -58551904800000
I found a solution, after I read Josh and ghusse answers and advice:
Use getFullYear(), instead getYear(), and all will work O.K.
Apparently, you have a problem with your end dates :
Thu Jul 26 114 00:00:00 GMT+0200
Does not mean 21/07/2014 but 21/07/114
According to the doc, here are 2 correct ways of creating your date:
var endDate = new Date(21, 6, 2014);
// Or a string corresponding to a version of ISO8601
var endDate = new Date('2014-07-21T00:00:00z+3');

How can I turn this date type into a normal date type ? javascript [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 9 years ago.
How can I turn this Fri Feb 21 2014 00:00:00 GMT-0800 (Pacific Standard Time) into just this 2014-02-21 using javascript ?
You can break the string into its parts, then format the bits into what you need:
// Reformat string like: Fri Feb 21 2014 00:00:00 GMT-0800 (Pacific Standard Time)
// do yyyy-mm-dd
function reformatDateString(s) {
function z(n){return (n<10?'0':'') + n;}
var months = {jan:'01', feb:'02', mar:'03', apr:'04', may:'05', jun:'06',
jul:'07', aug:'08', sep:'09', oct:'10', nov:'11', dec:'12'};
s = s.split(/[ :]/g);
return s[3] + '-' + months[s[1].toLowerCase()] + '-' + z(s[2]);
}
You can use the Date constructor, but it's not necessary here. Using the constructor to parse strings is problematic since the string in the OP doesn't fit the format specified in ES5 (which is not supported by all browsers in use) and parsing is otherwise implementation dependent.
So to use Date you need to parse the parts anyway, resulting in many extra function calls.
Using standard string/array manipulation
var timeStamp = 'Fri Feb 21 2014 00:00:00 GMT-0800 (Pacific Standard Time)',
months = {
Jan: 1,
Feb: 2,
Mar: 3,
Apr: 4,
May: 5,
Jun: 6,
Jul: 7,
Aug: 8,
Sep: 9,
Oct: 10,
Nov: 11,
Dec: 12
},
parts = timeStamp.split(' ', 4).slice(1),
myStamp;
function pad(val) {
if (val < 10) {
val = '0' + val;
}
return val;
}
parts[0] = months[parts[0]];
parts.unshift(parts.pop());
parts[1] = pad(parts[1]);
parts[2] = pad(parts[2]);
mystamp = parts.join('-');
console.log(mystamp);
Output
2014-02-21
On jsFiddle
The date Fri Feb 21 2014 00:00:00 GMT-0800 is in standard RFC 2822 format so you can create a new date using new Date() passing it as a parameter. This will convert it into UTC (milliseconds since 1/1/1970) which you can manipulate.
You can then convert UTC into ISO 8601 extended format (2014-02-21T00:00:00.000Z) with the toISOString() method and get the text before the T:
var utcDate = new Date('Fri Feb 21 2014 00:00:00 GMT-0800');
var isoExtendedDate = utcDate.toISOString();
var isoSimpleDate = isoExtendedDate.split("T")[0];
I'm unable to delete this since the OP chose it as the answer (OP please choose Xotic750's) answer.
See Xotic750's answer

Categories

Resources