0

I would like to know how to compare any two different date strings in javascript.

Two Date Strings, d1 and d2 are always in the format , dd mmm yyyy and yyyy-mm-dd

How to compare datestring whether is same or not in javascript Below datestring is example,

var d1 = "12 Feb 1990" 
var d2 = "1990-02-12"

if(d1.split(' ').[1] === new Date().toLocaleString('en-US', {month: 'short'})){
  return true;
}
else {
return false
}


Bergi
  • 630,263
  • 148
  • 957
  • 1,375
sen
  • 91
  • 7

2 Answers2

-1

If you want to compare only the date string then the .toDateString method is all you need. Something like

You can read more about the toDateString method on MDN

let d1 = "12 Feb 1990" 
let d2 = "1990-02-12"

const d1String = new Date(d1).toDateString()
const d2String = new Date(d2).toDateString()
if (d1String === d2String) {
  console.log('Equal');
} else {
  console.log('Not equal');
}
RobG
  • 142,382
  • 31
  • 172
  • 209
Jjagwe Dennis
  • 1,643
  • 9
  • 13
  • 1
    "1990-02-12" is parsed as UTC, "12 Feb 1990" (if parsed correctly) is not, so for users with a positive offset they are different local dates. For users with a negative offset, they are different UTC dates. See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Jan 06 '22 at 21:38
  • Hey, @RobG thank you for sharing. I don't know whether you tried out my answer before downvoting it? As per the MDN docs, Unlike `toString`, `toDateString` returns consistent results. It ignores the time and returns only the Date string so the timezone will have no effect. The person who asked the question asked to compare Date strings and not DateTime strings – Jjagwe Dennis Jan 07 '22 at 08:05
  • 1
    Perhaps you misunderstood the comment. I've converted your code to a runnable snippet. Set your PC to a timezone with a negative offset and the result is "Not equal". If your PC has a positive offset, you can test that the UTC dates are not equal. – RobG Jan 07 '22 at 11:03
  • Hey Rob, Thanks again for sharing this. I misunderstood the comment. Converting to a negative offset behaves as you described. I didn't know that this behaved this way since I have only dealt with days in +ve offsets. Today I learned something new. – Jjagwe Dennis Jan 07 '22 at 14:43
-2

Here's some code that reorders the "1990-02-12" string and compares it to the "12 Feb 1990" one:

function d2ToNormalDate(string) {
    return string.split("-")[1] + "-" + string.split("-")[2] + "-" + string.split("-")[0];
}
function areDateStringsEqual(string1, string2) { 
    // Expects string1 to be a string like "12 Feb 1990" and
    // expects string2 to be a string like "1990-02-12".
    // returns a boolean.
    return new Date(d2ToNormalDate(string2)).toString() === new Date(string1).toString()
}
user1280483
  • 470
  • 4
  • 11
  • `new Date('02-12-1990')` returns an invalid Date in Safari at least, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) Parsing a string to create another string that is then parsed by the built–in parser doesn't make much sense, parse it once and pass values directly to the Date constructor. – RobG Jan 06 '22 at 21:41