-1

we are trying to format date in an e-commerce JavaScript application. We have tried using appending "00:00:00" to the date String. But in IE 11, with that string we are getting a Invalid Date error.

So we have tried a few more options but the the date Object we are getting is unexpected:

1. new Date(2017, 02, 10, 00, 00, 00, 000) -->
   Fri Mar 10 2017 00:00:00 GMT-0800 (Pacific Standard Time)

2. new Date("2017-02-10T00:00:00.000Z") -->
   Thu Feb 09 2017 16:00:00 GMT-0800 (Pacific Standard Time)

Is the only effective way is to use "moment.js" to solve these issues.

Thanks and Regards

kallada
  • 1,671
  • 1
  • 26
  • 53
  • 2
    a date is a date you do not format a date; you format an string. Can you please share what is the "string" format you want for your date? Or what you want is trim the time from your date? – Dalorzo Feb 10 '17 at 17:55
  • Please find the details here: https://jsfiddle.net/vhLqtu35/17/ – kallada Feb 10 '17 at 19:59
  • 1
    "*Is the only effective way is to use "moment.js" to solve these issues.*" No. A library can help but is not required. Probably a duplicate of [*Where can I find documentation on formatting a date in JavaScript?*](http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript?s=1|6.7038) or [*How to format a JavaScript date*](http://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date?s=2|5.7726). – RobG Feb 10 '17 at 20:52
  • "unexpected"... what about those results is unexpected? Look at [the documentation for the Date object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) and it will tell you exactly how it's using those values you've passed in. This also looks like a question about parsing a date rather than formatting. – Heretic Monkey Feb 10 '17 at 23:03

1 Answers1

1

It is unclear whether you're asking about creating dates or formatting them as strings. But there is nothing unexpected here.

new Date(2017, 02, 10, 00, 00, 00, 000)

You should not use leading zeros on numbers. In some instances in older browsers it can indicate octal rather than decimal. Anyway, the above will produce a "local" date for 10 March 2017 at 00:00:00 based on the host system time zone setting. Yours seems to be set for UTC-08:00.

Given:

new Date("2017-02-10T00:00:00.000Z")

you're asking the Date constructor to parse a string, something it's notoriously bad at. However, browsers older than IE 8 should parse it correctly and produce a date for 10 March 2017 at 00:00:00 UTC, which has a timezone offset of 00:00.

Since your timezone is -08:00, when you write it to output the values will be adjusted for the host timezone (i.e. local) so you see a date for 9 March 2017 at 16:00:00 (or 4 pm).

Note that using the built–in Date.prototype.toString will result in an implementation dependent string that may be different in each host. If you need information on formatting a date, see Where can I find documentation on formatting a date in JavaScript? or How to format a JavaScript date.

Is the only effective way is to use "moment.js" to solve these issues.

It's unclear what issues you're trying to solve, however a good, light, parsing and formatting library is fecha.js. If you also want to do arithmetic, you might find moment.js suitable. But it's not difficult to write a function to parse specific formats or create suitable strings, see the above links.

Community
  • 1
  • 1
RobG
  • 124,520
  • 28
  • 153
  • 188