-1

I am getting a time format, which is the following 2022-12-24T16:00:00Z and I want to convert it to a timestamp, I found a format, but it only works by removing the letters "T" and "Z" that are embedded inside the time. Which doesn't work for me, because I get it automatically, and my intention is that's how I get it, automatically convert it to timestamp

    const str = "2022-12-24T16:00:00Z";

    const [dateComponents, timeComponents] = str.split(" ");
    console.log(dateComponents); // ️ 
    console.log(timeComponents); // ️ 

    const [year, month, day] = dateComponents.split("-");
    const [hours, minutes, seconds] = timeComponents.split(":");

    const date = new Date(+year, month - 1, +day, +hours, +minutes, +seconds);
    console.log(date); // ️ 

    // ✅ Get timestamp
    const timestamp = date.getTime();
    console.log(timestamp); // ️ 

How could it be solved, without having to enter it without those letters within the time

convert data to timestamp, but this data has a different format, which if you remove the excess, it works, but if not, no.

MatiST00
  • 11
  • 1
  • 2
  • 2
    `new Date(str).getTime()`?! This is an ISO 8601 date string. See [Parsing ISO 8601 date in Javascript](/q/4829569/4642212). What doesn’t work? – Sebastian Simon Nov 17 '22 at 22:22
  • If I replace `2022-12-24T16:00:00Z` by `2022-12-24 16:00:00`, it works, but it doesn't work for me, because I get it the first way. – MatiST00 Nov 17 '22 at 22:26
  • 1
    `new Date("2022-12-24T16:00:00Z").getTime() === 1671897600000`. Isn’t that the result you expect? What browser are you on that doesn’t support the standard ISO 8601 format in the `Date` constructor? Are you expecting the timestamp in local time instead of UTC? There are [solutions for that](/q/15141762/4642212), but note that `Z` signifies UTC. Are you really just asking about `str.split(" ")` which should be `str.slice(0, -1).split("T")`? – Sebastian Simon Nov 17 '22 at 22:28
  • "If I replace `2022-12-24T16:00:00Z` by `2022-12-24 16:00:00`" - by doing that you are potentially changing the date/time as you are removing the timezone - the first is UTC time, the second is _local_. If you are not in UTC+0 timezone then these two strings will result in different timestamps. – MrWhite Nov 17 '22 at 22:35
  • 1
    [What Do You Mean “It Doesn’t Work”?](//meta.stackexchange.com/q/147616/289905) _“It doesn’t work”_ isn’t a problem statement and is meaningless without knowing your _desired_ results and your _actual_ results. – Sebastian Simon Nov 17 '22 at 22:47

1 Answers1

1

The Date constructor can parse this format:

console.log(new Date("2022-12-24T16:00:00Z").toUTCString());
console.log(new Date("2022-12-24T16:00:00Z").getTime());

If you need to parse both the ISO 8601 format and a format with spaces, you can replace the appropriate line in your code with this:

const [dateComponents, timeComponents] = str.split(new RegExp("[a-z]+|\\s+", "i"));

But be aware, the Z is a time zone, in this case, indicating GMT time.

Benjamin Penney
  • 637
  • 3
  • 10
  • I don't understand, I have it like this, but when I pass it it doesn't work, but when I remove those letters, yes. – MatiST00 Nov 17 '22 at 22:28
  • 2
    @MatiST00 What do you mean exactly by "it doesn't work"? Are you getting an error? An incorrect/unexpected timestamp? Or something else? – MrWhite Nov 17 '22 at 22:45