40

I have a string that contains 8 digits that represent a date. For example:

20120515

I'd like to compare it with today's date, created in this manner:

var currentDate = new Date();

How can I convert the "8 digit date string" to a suitable date format in order to compare it to currentDate?

Fredrik Ljung
  • 1,445
  • 13
  • 28
kirbyjwilson
  • 401
  • 1
  • 4
  • 3
  • 2
    `var dt="20120515", dtObj=new Date(dt.substr(0,4),dt.substr(4,2)-1,dt.substr(6,2));` – ashleedawg Sep 24 '21 at 20:30
  • ...and then if you need it more "human-friendly" you can use something like [`dtObj.toString().substr(0,15)`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Date/toString) or [`dtObj.toLocaleDateString()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString). – ashleedawg Oct 17 '22 at 02:29

4 Answers4

62

Use the substring method and substring off 4 elements and assign it to your new date for the year. Then substring off two elements at a time and store the month and date accordingly.

var dateString  = "20120515";
var year        = dateString.substring(0,4);
var month       = dateString.substring(4,6);
var day         = dateString.substring(6,8);

var date        = new Date(year, month-1, day);
var currentDate = new Date();

Now you can compare the two dates with the normal operators.

Christiaan Westerbeek
  • 10,619
  • 13
  • 64
  • 89
firexfighterx
  • 649
  • 4
  • 2
  • 1
    I think you need to add one to the second parameter passed to [`.substring()`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/substring) - it extracts from the first index up to _but not including_ the second index, so `.substring(0,3)` only takes the first three characters... – nnnnnn May 15 '12 at 23:55
  • 2
    The Date constructor's month parameter is zero based. You're calculating towards the date 14th of June 2012 instead of May. I'm surprised to be the first to notice after 1 year. I changed your code. – Christiaan Westerbeek May 08 '13 at 13:36
25

If you want a small date library you can use moment.js.

var a = moment("20120515", "YYYYMMDD");
// then use any of moment's manipulation or display functionality
a.format("MMM Do YYYY"); // May 15th 2012
a.fromNow(); // 14 hours ago
a.calendar(); // Today at 12:00 AM
timrwood
  • 10,611
  • 5
  • 35
  • 42
2

To correctly handle the local time zone, it must explicitly summed to the calculated time

function dateStringToDate(dateString) {
  try {
    var year = dateString.substring(0, 4);
    var month = dateString.substring(4, 6);
    var day = dateString.substring(6, 8);
    var date = new Date(year, month - 1, day);
    const offset = date.getTimezoneOffset()
    date = new Date(date.getTime() - (offset * 60 * 1000));
    return date;
  } catch (error) {
    return null;
  }
}

function dateStringToDate(dateString) {
  try {
    var year = dateString.substring(0, 4);
    var month = dateString.substring(4, 6);
    var day = dateString.substring(6, 8);
    var date = new Date(year, month - 1, day);
    const offset = date.getTimezoneOffset()
    date = new Date(date.getTime() - (offset * 60 * 1000));
    return date;
  } catch (error) {
    return null;
  }
}

console.log(dateStringToDate("20211212"))
console.log(dateStringToDate("20211213"))
console.log(dateStringToDate("20211214"))
loretoparisi
  • 15,724
  • 11
  • 102
  • 146
2

...some other "one-liner" ways to accomplish this:

(They take a value like dts='20020704'; and return date object [dt].)

var dt=new Date(dts.slice(0,4), (dts[4]+dts[5])-1, dts[6]+dts[7]);

...or...

var m=dts.match(/(....)(..)(..)/), dt=new Date(m[1],m[2]-1,m[3]);

...or...

var m=dts.match(/.{1,2}/g), dt=new Date(m[0]+m[1],m[2]-1,m[3]);

The last one's shortest, but the first is probably most efficient, since it doesn't use regex (but that's irrelevant, unless you're processing LOTS of data using this). I like the middle one best since it's easy to see what's happening.

ashleedawg
  • 20,365
  • 9
  • 72
  • 105