Tutorialspoint.dev

JavaScript | date.toISOString() function

The date.toISOString() is an inbuilt function in JavaScript which is used to convert the given date object’s contents into a string in ISO format (ISO 8601) i.e, in the form of (YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ).The date object is created using date() constructor.

Syntax:

dateObj.toISOString()

Note: In the above syntax, DateObj is a valid Date object created using Date() conctructor.

Parameters:

  • This function does not take any parameter. It is just used along with a Date object created using Date() constructor.

Return Values:



  • It returns the converted string of Date() constructor contents into ISO format (ISO 8601).

Below program illustrate the date.toISOString() function:-

<script>
// Here a date has been assigned
// while creating Date object
var dateobj = new Date('October 15, 1996 05:35:32');
  
// Contents of above date object is converted
// into a string using toISOString() function.
var B = dateobj.toISOString();
  
// Printing the converted string.
document.write(B);
</script>

/div>

Output:

> "1996-10-15T00:05:32.000Z"

Errors and Exceptional cases associated with this function:
Code #1:
Here nothing as parameter is passed while creating date object but still toISOString() function return current day name, month name, date, year and time.

<script>
// Here nothing has been assigned
// while creating Date object
var dateobj = new Date();
  
// Contents of above date object is
// converted into a string using toISOString() function.
var B = dateobj.toISOString();
  
// Printing the converted string.
document.write(B);
</script>

Output:

> "2018-04-23T10:26:00.996Z"

Important Points

  • Months, Dates, hours, minutes, seconds, milliseconds should all be in their respective range of 0 to 11, 1 to 31, 0 to 23, 0 to 59, 0 to 59, 0 to 999 respectively.


This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter