'How do I pass the correct format for the Date in ts?

I want to pass the value in a local storage service but am unable to parse because of no date format support. How should I proceed?

 description,
      TaskDate: ,
      TaskTime: {
        hours: 0,
        minutes: 0


Solution 1:[1]

You can serialize/deserialize a date for use with localStorage like this:

TS Playground

/** Serialize/Deserialize a Date for use with localStroage */
function dateSerde (iso: string): Date;
function dateSerde (date: Date): string;
function dateSerde (input: string | Date) {
  return typeof input === 'string' ? new Date(input) : input.toISOString();
}


// Use:

const storageKey = 'my-date';
const date = new Date();

const serialized = dateSerde(date);
localStorage.setItem(storageKey, serialized);

const item = localStorage.getItem(storageKey);

if (item) {
  const deserialized = dateSerde(item);
  console.log({equal: date.getTime() === deserialized.getTime()}); // true
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jsejcksn