0

I want to convert the string to a date object. The time may be in different formats, such as 2020-01-01 or 2020/01/01, or it may be accurate to the minute.

Time parsing in JavaScript sometimes gives strange results.

new Date("2021-01-01") // Fri Jan 01 2021 09:00:00 GMT+0900
new Date("2021-01-01 00:00") // Wed Jan 01 2020 00:00:00 GMT+0900

The first date I enter is thought to be UTC, and the second is thought to be local.

Is there any way to properly parse them?

Jannchie
  • 690
  • 6
  • 18
  • 1
    Consider using [momentjs](https://momentjs.com/) for easy manament over time functions. – Adarsh Mohan Jan 30 '21 at 14:33
  • 1
    The second one is not a standard compliant string - JavaScript specs say that `T` is needed between the date and the time, a space will lead to implementation specific parsing. Even if it was, the specs are clear - *date* strings are parsed as UTC, *date-time* strings are parsed as local time. – VLAZ Jan 30 '21 at 14:39
  • My requirement is a string to time conversion. The `new Date()` method appears to return different times in different cases, such as "2020-01-01" and "2020-01-01 00:00", two seemingly identical times that resolve differently. Maybe I should use moment.js – Jannchie Jan 30 '21 at 14:49
  • As I said, a *date* string and a *date-time* string are indeed handled differently as per the specification. You need to use *the same* format for repeatable results. See [Converting a string to a date in JavaScript](https://stackoverflow.com/q/5619202) and [I have a UTC string and I want to convert it to UTC Date Object in JavaScript](https://stackoverflow.com/q/40768606) – VLAZ Jan 30 '21 at 14:52
  • @VLAZ You are right, but I think the result is a bit confusing. In moment.js, or human's eye, "2020-01-01" and "2020-01-01 00:00" are the same time. – Jannchie Jan 30 '21 at 14:57
  • Maybe I've looked at too much timestamps but without a timestamp, I'd only compare two dates if they are clearly from the same source. I've seen multiple log files on the same machine where the same event has been timestamped with different times. So, within one log 14:00 clearly comes before 14:05 but it might actually come after an entry from the other log file which says it's from 15:00 – VLAZ Jan 30 '21 at 15:01
  • @VLAZ Using timestamps is great. In my case, I'm handling user's input and I want user can enter the date more flexible. So I don't have a choice. – Jannchie Jan 30 '21 at 15:08
  • this is not realistic, if you want to have the same Date/Time, you need to change your computer's preference time zone based on the second DT value of date – Mister Jojo Jan 30 '21 at 15:24

3 Answers3

0

You can use new Date.getString() to get the time in human readable format for your time zone. If you want the UTC date, you can use new Date.getUTCString().
If you want additional info, you can check out this MDN page for descriptions of the Date methods.
Here's a working demo:

var localDate = new Date("2021-01-01 00:00").toString()
var UTCDate = new Date("2021-01-01 00:00").toUTCString()
console.log(`Local Date: ${localDate}`)
console.log(`UTC Date: ${UTCDate}`)
0

In the end, although this would introduce additional complexity, I used Moment.js.

const a = moment("2020-01-01 00:00").toDate();
const b = moment("2020-01-01").toDate();
console.log(a.getTime() == b.getTime());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
Jannchie
  • 690
  • 6
  • 18
-1

Yes, you can apply methods to a Date object like this:

const d = new Date()

d.getTime();
d.getDay()
d.getFullYear()

etc. Here is more info

Josh Merrian
  • 291
  • 3
  • 11