How do I compare two string Datetimes? [duplicate] - javascript

This question already has answers here:
Compare two dates with JavaScript
(43 answers)
Closed 2 years ago.
I have Datetimes formated as "16 Apr 2020 02:07 PM CST" and I need to compare it to another datetime just like that to know which date came first.
What I tried to do until now was:
var firstDate = "16 Apr 2020 02:07 PM CST";
var secondDate = "23 Apr 2020 06:07 AM CST";
var diff = Math.abs(firstDate - secondDate);
That or I tried to check if one was greater than the other and got similar results.

You should check time in milliseconds (timestamp) to compare dates:
var firstDate = new Date("16 Apr 2020 02:07 PM CST");
var secondDate = new Date("23 Apr 2020 06:07 AM CST");
if (firstDate.getTime() < secondDate.getTime()) {
console.log('First Date came first');
} else {
console.log('Second Date came first');
}

Related

new Date returned invalid in safari but chrome? [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 2 years ago.
new Date('2020-08-18 07:52') is working in Chrome, it returned
Tue Aug 18 2020 07:52:00 GMT+0800 (Malaysia Time)
but safari gave me invalid date? what's the best way to fix this? this bug in safari is breaking my entire app.
without need for tools/plugins/packages, I would simply separate the received date:
var apiDate = '2020-08-18 07:52'
var [ date, time ] = apiDate.split(' ')
var [ year, month, day ] = date.split('-')
var [ hour, minute ] = time.split(':')
var newDate = new Date(year, month - 1, day, hour, minute, 0)
// Tue Aug 18 2020 07:52:00
I would just be careful about TimeZones, as your date has no time zone description ...
Note
from the code above, one might think that subtracting 1 from a string is invalid, but that's the caveats of javascript...
"08" + 1 = "081"
"08" - 1 = 7
if the format is always the same, you can also do:
var apiDate = '2020-08-18 07:52'
var newDate = new Date(`${apiDate.replace(' ', 'T')}:00`) // '2020-08-18T07:52:00'
// Tue Aug 18 2020 07:52:00

JavaScript Date format is showing wrong [duplicate]

This question already has answers here:
new Date('dd/mm/yyyy') instead of newDate('mm/dd/yyyy') [duplicate]
(3 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 2 years ago.
I have an API which gives the date format like 01/04/2020 00:17:26 and we are using the JavaScript code below:
function getLocalTime(timestamp){
try {
let localTime = new Date(timestamp.replace('at ','') + ' GMT+530');
return localTime;
} catch(e){
return timestamp;
}
}
vat date = getLocalTime('01/04/2020 00:17:26');
console.log(date);
The above code is returning Sat Jan 04 2020 00:17:26 GMT+0530 (India Standard Time) But expected result will be as like Wed Apr 01 2020 00:17:26 GMT+0530 (India Standard Time)
Please help us with this.
The issue is the wrong date format you are using.
Your format: DD/MM/YYYY
Expected format: MM/DD/YYYY
Still, if the input format you are expecting (from the user) is DD/MM/YYYY (e.g. 01/04/2020 00:17:26), then you can use regex to extract the date info like so
function getLocalTime(timestamp){
try {
const datePart = timestamp.match(/^(\d{2})\/(\d{2})\/(\d{4})/);
const day = datePart[1];
// month goes from 0 to 11
const month = datePart[2] - 1;
const year = datePart[3];
const localTime = new Date(timestamp.replace('at ','') + ' GMT+0530');
localTime.setFullYear(year);
localTime.setMonth(month);
localTime.setDate(day);
return localTime;
} catch(e){
return timestamp;
}
}
const date = getLocalTime('01/04/2020 00:17:26');
console.log(date);
Update
#RobG proposed a fantastic solution (see in comments below).
function getLocalTime(timestamp) {
try {
const matches = timestamp.split(/\D+/); // or use timestamp.match(/\d+/g)
return new Date(Date.UTC(matches[2], matches[1]-1, matches[0], matches[3]-5, matches[4]-30, matches[5]));
} catch(e){
return timestamp;
}
}
const date = getLocalTime('01/04/2020 00:17:26');
console.log(date);

new Date() always read dd/mm/yyyy as mm/dd/yyyy(month first) hence day and month values get mixed resulting in NaN/Error [duplicate]

This question already has answers here:
How to convert dd/mm/yyyy string into JavaScript Date object? [duplicate]
(10 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 3 years ago.
When converting date for example:
var dateObj = new Date("10/01/2019");
console.log(dateObj);
returns Tue Oct 01 2019 00:00:00 i.e. it takes in the day as month and likewise with the month value
How to make new Date() to take dd/mm/yyyy ??
Answer is here: https://stackoverflow.com/a/33299764/6664779
(From original answer) We can use split function and then join up the parts to create a new date object:
var dateString = "23/10/2019"; // Oct 23
var dateParts = dateString.split("/");
// month is 0-based, that's why we need dataParts[1] - 1
var dateObject = new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0]);
document.body.innerHTML = dateObject.toString();

Compare two date object in angular 4 [duplicate]

This question already has answers here:
Compare two dates with JavaScript
(43 answers)
Closed 4 years ago.
I tried to compare two date objects in angular 4 but it always returning false
below are my code snippet
CheckForHoliday(d: Date) {
let simpleDate = new Date(d.getFullYear(), d.getMonth(), d.getDay());
let hDate = new Date(2018, 9, 1);
console.log(simpleDate + "==========" + hDate);
console.log(hDate === simpleDate);
return (hDate === simpleDate);
}
The output is as below
Mon Oct 01 2018 00:00:00 GMT+0530 (India Standard Time)==========Mon Oct 01 2018 00:00:00 GMT+0530 (India Standard Time)
false
Any Idea why it returning false when the printed values look same?
You could do a date.getTime() and then compare the two numbers
You should compare the dates in the date Objects. If you call the getDate() method on the date Objects you will get true.
hDate.getDate() === simpleDate.getDate() //=> returns true
new Date will return an object and you cannot compare objects like that. Try converting both of them to string before comparing them.
Do it like this
CheckForHoliday(d: Date) {
let simpleDate = new Date(d.getFullYear(), d.getMonth(), d.getDay());
let hDate = new Date(2018, 9, 1);
console.log(simpleDate + "==========" + hDate);
console.log(hDate === simpleDate);
return (hDate.toString() === simpleDate.toString());
}

How can remove time from Date string [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 8 years ago.
var x = new Date();
myVar = x.toString();
document.write(myVar);
// Sat Feb 14 2015 14:20:58 GMT+0100 (CET)
I want to remove the time <<14:20:58 GMT+0100 (CET)>>
Try ...
var x = new Date()
var myVar = x.toDateString();
This will only provide the date ...
Documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toDateString

Categories

Resources