-2

My date is 2022-01-19T13:00:56.000Z

I need to be able to replace some characters in this, so I need to convert it to a string.

However it loses its format if I use toString():

console.log(myDate.toString())
// output is Wed Jan 19 2022 21:00:56 GM +0800 (Hong Kong Standard Time)

I want to convert the date 2022-01-19T13:00:56.000Z to "2022-01-19T13:00:56.000Z".

How to accomplish this?

Stiofán
  • 453
  • 5
  • 17
  • 1
    `myDate.toISOString()` – mplungjan Jan 22 '22 at 14:02
  • But what do you need to change? Perhaps you should keep it a date and do the changes using the date methods – mplungjan Jan 22 '22 at 14:03
  • @mplungjan that doesn't work unfortunately, as it's still not a real string, so you can't use functions like replace(). – Stiofán Jan 22 '22 at 14:05
  • 1
    That is untrue.. Please explain what you mean by not a real string? – mplungjan Jan 22 '22 at 14:05
  • to follow on from @mplungjan tempDate = new Date( myDate ).toISOString() Should do it, but I don't think you are doing the right thing. – BuzzCloudAU Jan 22 '22 at 14:08
  • I don't see the difference between `2022-01-19T13:00:56.000Z` and `"2022-01-19T13:00:56.000Z"`. A date is internally not stored as a string `2022-01-19T13:00:56.000Z`. This is just the string representation (that means, it's already a string). A date doesn't contain a format or a timezone. I think this question is some kind of misunderstanding. – jabaa Jan 22 '22 at 14:11
  • 1
    @mplungjan You're correct, I had a syntax error. – Stiofán Jan 22 '22 at 14:14
  • @jabaa you can't use replace() and split() on a date, it must be a string. – Stiofán Jan 22 '22 at 14:14
  • I already voted to close as typo-type ;) – mplungjan Jan 22 '22 at 14:15
  • I never said, you could use replace or split on a date. But `2022-01-19T13:00:56.000Z` is a string (with format and timezone), not a date. A date doesn't contain a format or a timezone. There is no original formatting in a date. – jabaa Jan 22 '22 at 14:20
  • @jabaa let me try to explain. The date 2022-01-19T13:00:56.000Z is not a string so it cannot use functions like replace() or split(). To use these functions I need to convert the date to a string. But using toString() converts 2022-01-19T13:00:56.000Z to "Wed Jan 19 2022 21:00:56 GM +0800 (Hong Kong Standard Time)". So my question was how to I convert 2022-01-19T13:00:56.000Z to a string but not change it to "Wed Jan 19 2022 21:00:56 GM +0800 (Hong Kong Standard Time)". – Stiofán Jan 22 '22 at 15:13
  • If you're just trying to get a formatted timestamp from a Date, there are already many, many questions about that. Perhaps see [*How to ISO 8601 format a Date with Timezone Offset in JavaScript?*](https://stackoverflow.com/questions/17415579/how-to-iso-8601-format-a-date-with-timezone-offset-in-javascript) – RobG Jan 23 '22 at 12:16

1 Answers1

1

toISOString DOES produce a string you can manipulate

const date = new Date("2022-01-19T13:00:56.000Z")
console.log(date.toLocaleString());

// Add 6 hours to the time
date.setHours(date.getHours() + 6);

const isoString = date.toISOString();

// Intermediate result
console.log(isoString);

// Fix the offset
const ZPlus6 = isoString.replace("Z","+0600");

// Adjusted timestamp
console.log(ZPlus6);
// Produces the same date and time as original
console.log(new Date(ZPlus6).toLocaleString());
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • I changed to add 6 hours too - OP said he could not access the date as a string, so I just posted code to show how to replace the string representation – mplungjan Jan 23 '22 at 08:50
  • Ooops did not use getHours/setHours when adding 6 hours – mplungjan Jan 23 '22 at 12:12
  • 1
    Not really sure what the OP wants but seems you've answered the question. I tidied up some things just in case others stumble across this. Your method of adjusting the time is OK, just need to use the right value. :-) – RobG Jan 23 '22 at 12:15