Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Date using forward-slashes vs hyphens [duplicate]

When I'm creating a new Date object and pass in a date using hyphens

new Date("2015-07-02") // I get Thu Jul 02 2015 01:00:00 GMT+0100 (IST)

and when I use forward slashes

new Date("2015/07/02") // I get Thu Jul 02 2015 00:00:00 GMT+0100 (IST)

notice the time difference: 01:00:00 for hyphens and 00:00:00 for forward slashes

this breaks my code :(

Why this is happening? Any workaround for this? (Should I just set time to 00:00:00 when using hyphens?)

I need to be able to compare dates that have forward-slashes with dates that have hyphens and I'm not sure I might need to compare dates with some other symbols.

Is this happening to hyphens only?

Thanks.

like image 200
Eugene Avatar asked Apr 09 '14 10:04

Eugene


People also ask

What function would you use to replace slashes or dashes in a list of dates?

If your dates are formatted with forward slashes (/), you are going to enter a forward slash into BOTH the Find what and Replace with fields. If your dates are formatted with dashes (-), then use dashes. Then click Replace All. (The keyboard shortcut for Replace All is Alt + A .)

What function could you use to replace slashes?

As the forward slash (/) is special character in regular expressions, it has to be escaped with a backward slash (\). Also, to replace all the forward slashes on the string, the global modifier (g) is used.

What is new date () format in JavaScript?

The most used method to get the date in JavaScript is the new Date() object. By default, when you run new Date() in your terminal, it uses your browser's time zone and displays the date as a full text string, like Fri Jul 02 2021 12:44:45 GMT+0100 (British Summer Time).


1 Answers

If a recent browser can interpret the date string as ISO-8601 - it will do it.

examples :

 YYYY (eg 1997)
 YYYY-MM (eg 1997-07)
 YYYY-MM-DD (eg 1997-07-16)
 YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
 YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
 YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)

With this format, your date/time string is interpreted as UTC(!!!).

You should Stick to "YYYY-MM-DD" for your date strings whenever possible

like image 70
Royi Namir Avatar answered Sep 19 '22 01:09

Royi Namir