How to convert JavaScript date to string

convert JavaScript date to string: Developers often want most of the data and information in the form of string data types. For
example, displaying as text in HTML or in the databases, it is often more convenient to store the dates in the form of string data types.

toString() method

toString() is a built in method in javascript.toString() method converts the date objects in the
form of string.

Syntax

toString(date);

output format

date

Parameters

By default, there is no necessary parameter that needs to be passed.

    • If no parameter is passed then it will return the current date and time in the form of a
      string.
  • If the parameter is passed in any other format other than string data types then the
    javascript will return the date in string format for that date entered by the user.

Return value

Date and time in the form of string.

Example 1(no parameter)

[javascript]const d = new Date();
let text = d.toString();
console.log(text);[/javascript]

Output

Sun Apr 03 2022 10:36:28 GMT+0000 (GMT)

javascript-date-to-string-output

Explanation

  • In the above code, we have first defined a variable called “d” which is a user defined
    variable using var.
  • Then we initialized the javascript object by using the new Date() method.
  • We then defined another variable called “text”.
  • We stored the value of date in the form of a string and then stored the value in the “text”
    variable.
  • Finally, we printed the output on the screen with the help of the console.log()
    method.
  • The toString() method here converts the data types of the date into the String
    data types.

toString() method with parameters

If parameters are given then javascript will return the given dates in the form of string. Note that
it will not return the current date. It will return only that date that is entered by you in the form of
the parameter.

Syntax

To specify only the year:

[javascript]new Date(value);[/javascript]

To specify only the year and month:

[javascript]new Date(year, indexOfMonth);[/javascript]

To specify only the year and month, day:

[javascript]new Date(year, indexOfMonth, day);[/javascript]

To specify only the year and month, day, hours:

[javascript]new Date(year, indexOfMonth, day, hours);[/javascript]

To specify only the year and month, day, hours, minutes:

[javascript]new Date(year, indexOfMonth, day, hours, minutes);[/javascript]

To specify only the year and month, day, hours, minutes, seconds:

[javascript]new Date(year, indexOfMonth, day, hours, minutes, seconds);[/javascript]

To specify only the year and month, day, hours, minutes, seconds, milliseconds:

[javascript]new Date(year,indexOfMonth, day, hours, minutes, seconds, milliseconds);[/javascript]

Output format:

day month date year hour:minute: seconds UTC time

Example 1

[javascript]const d = new Date(2020,2);
let text = d.toString();
console.log(text);[/javascript]

output

Sun Mar 01 2020 00:00:00 GMT+0530 (India Standard Time)

Example 2

[javascript]const d = new Date(2020,2,2);
let text = d.toString();
console.log(text);[/javascript]

Output

Mon Mar 02 2020 00:00:00 GMT+0530 (India Standard Time)

Explanation

The output format of all the above will be the same. If all the parameters are not specified
javascript will by default assume the initial values. For an example of the hour:time: seconds is
not mentioned javascript will assume the value as 00:00:00.

Write for us
GeekyBeginners articles are written by software geeks like you. If you also would like to contribute to GeekyBeginners by writing paid articles, you can check the write for us page.

Leave a Comment