📜  js toisostring - Javascript (1)

📅  最后修改于: 2023-12-03 15:32:21.933000             🧑  作者: Mango

JS toISOString - JavaScript

The toISOString() method is a built-in date method in JavaScript that returns a string in ISO format (International Organization for Standardization).

Description

The toISOString() method returns a string representation of the date in ISO format. This format is widely used for exchanging data between systems and is recognized by most programming languages.

Syntax
dateObj.toISOString()
Parameters

The toISOString() method does not take any parameters.

Return Value

The toISOString() method returns a string representing the date in the following format:

yyyy-mm-ddThh:mm:ss.sssZ

Where:

  • yyyy - year (four digits)
  • mm - month (two digits)
  • dd - day (two digits)
  • T - separator between date and time
  • hh - hours (two digits)
  • mm - minutes (two digits)
  • ss - seconds (two digits)
  • sss - milliseconds (three digits)
  • Z - UTC offset (always "Z" for UTC)
Examples
const date = new Date('2021-03-01T12:00:00Z')
const str = date.toISOString()

console.log(str) // "2021-03-01T12:00:00.000Z"

In this example, we create a new Date object with a date string in ISO format. We then call the toISOString() method on this object and store the resulting string in str. Finally, we log this string to the console.

Browser Support

The toISOString() method is supported in all modern browsers, including Chrome, Firefox, Safari, and Edge. It is also supported in Internet Explorer 9 and higher.

Conclusion

In conclusion, the toISOString() method is a useful built-in method in JavaScript that converts a Date object to a string in ISO format. This format is widely used for exchanging data between systems and is recognized by most programming languages.