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)

const d = new Date();
let text = d.toString();
console.log(text);

Output

Fri Mar 18 2022 14:22:31 GMT+0530 (India Standard Time)

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:

new Date(value);

To specify only the year and month:

new Date(year, indexOfMonth);

To specify only the year and month, day:

new Date(year, indexOfMonth, day);

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

new Date(year, indexOfMonth, day, hours);

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

new Date(year, indexOfMonth, day, hours, minutes);

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

new Date(year, indexOfMonth, day, hours, minutes, seconds);

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

new Date(year,indexOfMonth, day, hours, minutes, seconds, milliseconds);

Output format:

day month date year hour:minute: seconds UTC time

Example 1

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

output

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

Example 2

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

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.

Being a computer science engineer, I love blogging. In this blog, I will share some useful tips for wordpress, SEO, Coding Related Stuff and More. Kindly Subscribe to our Newsletter to get direct updates of the new posts on your Email.

Leave a Comment