Convert date to number javascript

Convert a String to a Date object in JavaScript #

Use the Date() constructor to convert a string to a Date object, e.g. const date = new Date('2022-09-24'). The Date() constructor takes a valid date string as a parameter and returns a Date object.

Copied!

const str = '2022-09-24'; const date = new Date(str); console.log(date); // 👉️ Sat Sep 24 2022

We used the Date() constructor to convert a string to a Date object.

If you get an invalid Date when creating the Date object, you need to format the string correctly before passing it to the Date() constructor.

If you have difficulties creating a valid Date object, you can pass 2 types of arguments to the Date() constructor:

  1. a valid ISO 8601 string, formatted as YYYY-MM-DDTHH:mm:ss.sssZ, or just YYYY-MM-DD, if you only have a date without time.
  2. multiple, comma-separated arguments that represent the year, month (0 = January to 11 = December), day of the month, hours, minutes and seconds.

Here is an example that splits an MM/DD/YYYY formatted string (could be any other format) and passes the values as arguments to the Date() constructor to create a Date object.

Copied!

const str = '09/24/2022'; const [month, day, year] = str.split('/'); console.log(month); // 👉️ 09 console.log(day); // 👉️ 24 console.log(year); // 👉️ 2022 const date = new Date(+year, +month - 1, +day); console.log(date); // 👉️ Sat Sep 24 2022

We split the date on each forward slash to get the values for the month, day and year.

Notice that we subtracted 1 from the month when passing it to the Date() constructor.

This is necessary because the Date constructor expects a zero-based value for the month, where January = 0, February = 1, March = 2, etc.

Here is another example, which creates a Date that also contains the hours, minutes and seconds.

Copied!

const str = '09/24/2022 07:30:14'; const [dateValues, timeValues] = str.split(' '); console.log(dateValues); // 👉️ "09/24/2022" console.log(timeValues); // 👉️ "07:30:14" const [month, day, year] = dateValues.split('/'); const [hours, minutes, seconds] = timeValues.split(':'); const date = new Date(+year, +month - 1, +day, +hours, +minutes, +seconds); // 👇️️ Sat Sep 24 2022 07:30:14 console.log(date);

We first split the date and time string on the space, so we can get the date and time components as separate strings.

We then had to split the date string on each forward slash to get the value for the month, day and year. Note that your separator might be different, e.g. a hyphen, but the approach is the same.

We also split the time string on each colon and assigned the hours, minutes and seconds to variables.

We then passed all of the values to the Date() constructor to create a Date object.

If you need to store a date string in your database, it's best to store the ISO 8601 representation of the date.

You can get the ISO formatted date by calling the toISOString() method.

Copied!

const str = '09/24/2022 07:30:14'; const [dateValues, timeValues] = str.split(' '); console.log(dateValues); // 👉️ "09/24/2022" console.log(timeValues); // 👉️ "07:30:14" const [month, day, year] = dateValues.split('/'); const [hours, minutes, seconds] = timeValues.split(':'); const date = new Date(+year, month - 1, +day, +hours, +minutes, +seconds); // 👇️️ Sat Sep 24 2022 07:30:14 console.log(date); // 👇️ "2022-09-24T04:30:14.000Z" (ISO 8601) console.log(date.toISOString());

The toISOString method returns a string of the date in the ISO 8601 format according to universal time.

The ISO string can easily be passed to the Date() constructor to create a new Date object.

How do you convert a date into a number?

1. Right click at the cell which contains the date you want to convert to number, and in the right-click menu, select Format Cells common. 2. In the Format Cells dialog, under Number tab, select Custom from the Category pane, then go to right section, enter mmddyyyy into the Type textbox.

How do I convert a date to a number in typescript?

How to convert date to number in typescript?.
const sampleDate = new Date(1994, 12, 10);.
console. log(sampleDate);.
console. log(Date. parse(sampleDate));.

How do I convert a date to a number in node JS?

The new Date() gives the current date and time. To convert the name to a number, we use the getTime() method. The getTime() method returns the number of milliseconds from midnight of January 1, 1970 (EcmaScript epoch) to the specified date.

What date format is dd mm yyyy in JavaScript?

To format a date as dd/mm/yyyy: Use the getDate() , getMonth() and getFullYear() methods to get the day, month and year of the date. Add a leading zero to the day and month digits if the value is less than 10 . Add the results to an array and join them with a forward slash separator.