0

I want to compare the actual date to a format like this that I'm receiving from a server: item.expires_date.slice "2016-11-28 22:10:57 Etc/GMT"

In javascript how could this be possible? specially for the part Etc/GMT

In the case I just wanted to compare 2016-11-28 how can I achieve this:

 var today = new Date().toISOString().slice(0, 10);

     if(item.expires_date.slice(0, 10) > today) {
 console.log("This item have expired");

     } else {

       console.log("this item has not expired" );
     }

       }

it does not work because it brings to item has not expired comparing dates: 2016-11-28 - 2016-12-28 Thanks!

arnoldssss
  • 468
  • 3
  • 9
  • 22

1 Answers1

2

Since "Etc/GMT" is the same as "GMT+00:00", you can remove it and create a Date object from the string:

var s = "2016-11-28 22:10:57 Etc/GMT";
var d = new Date(Date.parse(s.replace("Etc/", "")));
console.log(d.toString());

Then you can compare d to the current date.

001
  • 13,291
  • 5
  • 35
  • 66
  • Be careful, stripping the timezone part means it will be treated as local and hence represent a different moment in time in every time zone with a different offset. Is that what is required? Or should it always be GMT? – RobG Dec 28 '16 at 22:40
  • Etc/GMT without any offset is a UTC date and should be parsed as such, the above code will fail when the locale date is different from UTC (which it normally will be). See [this answer](http://stackoverflow.com/questions/7303580/understanding-the-etc-gmt-time-zone) for more about Etc/GMT. – Tomas Langkaas Dec 29 '16 at 08:25