How to convert date to string javascript

Date.prototype.toString()

The toString() method of Date instances returns a string representing this date interpreted in the local timezone.

Try it

Syntax

Return value

A string representing the given date (see description for the format). Returns «Invalid Date» if the date is invalid.

Description

The toString() method is part of the type coercion protocol. Because Date has a [@@toPrimitive]() method, that method always takes priority over toString() when a Date object is implicitly coerced to a string. However, Date.prototype[@@toPrimitive]() still calls this.toString() internally.

The Date object overrides the toString() method of Object . Date.prototype.toString() returns a string representation of the Date as interpreted in the local timezone, containing both the date and the time — it joins the string representation specified in toDateString() and toTimeString() together, adding a space in between. For example: «Thu Jan 01 1970 00:00:00 GMT+0000 (Coordinated Universal Time)».

Date.prototype.toString() must be called on Date instances. If the this value does not inherit from Date.prototype , a TypeError is thrown.

  • If you only want to get the date part, use toDateString() .
  • If you only want to get the time part, use toTimeString() .
  • If you want to make the date interpreted as UTC instead of local timezone, use toUTCString() .
  • If you want to format the date in a more user-friendly format (e.g. localization), use toUTCString() .
Читайте также:  Java read file program files

Examples

Using toString()

const d = new Date(0); console.log(d.toString()); // "Thu Jan 01 1970 00:00:00 GMT+0000 (Coordinated Universal Time)" 

Specifications

Browser compatibility

BCD tables only load in the browser

See also

Found a content problem with this page?

This page was last modified on Jun 1, 2023 by MDN contributors.

Your blueprint for a better internet.

MDN

Support

Our communities

Developers

Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.

Источник

Date.prototype.toDateString()

The toDateString() method of Date instances returns a string representing the date portion of this date interpreted in the local timezone.

Try it

Syntax

Return value

A string representing the date portion of the given date (see description for the format). Returns «Invalid Date» if the date is invalid.

Description

Date instances refer to a specific point in time. toDateString() interprets the date in the local timezone and formats the date part in English. It always uses the following format, separated by spaces:

  1. First three letters of the week day name
  2. First three letters of the month name
  3. Two-digit day of the month, padded on the left a zero if necessary
  4. Four-digit year (at least), padded on the left with zeros if necessary. May have a negative sign

For example: «Thu Jan 01 1970».

  • If you only want to get the time part, use toTimeString() .
  • If you want to get both the date and time, use toString() .
  • If you want to make the date interpreted as UTC instead of local timezone, use toUTCString() .
  • If you want to format the date in a more user-friendly format (e.g. localization), use toLocaleTimeString() .

Examples

Using toDateString()

const d = new Date(0); console.log(d.toString()); // "Thu Jan 01 1970 00:00:00 GMT+0000 (Coordinated Universal Time)" console.log(d.toDateString()); // "Thu Jan 01 1970" 

Specifications

Browser compatibility

BCD tables only load in the browser

See also

Found a content problem with this page?

This page was last modified on Jun 1, 2023 by MDN contributors.

Your blueprint for a better internet.

MDN

Support

Our communities

Developers

Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.

Источник

How to convert a date to a string in JavaScript

There are multiple ways to format a date in JavaScript. You can either use the built-in methods like toUTCString() and toISOString() , or the Intl.DateTimeFormat object.

The Date object in JavaScript exposes several built-in methods that you can use to display the date in different formats. By default, the toString() method outputs the date in full text string format:

const date = new Date(2021, 8, 14, 7, 14); console.log(date.toString()); // OR console.log(date); // Tue Sep 14 2021 07:14:00 GMT+0500 (Pakistan Standard Time) 
console.log(date.toUTCString()); // Tue, 14 Sep 2021 02:14:00 GMT 
console.log(date.toISOString()); // 2021-09-14T02:14:00.000Z 

Similarly, you can use toDateString() and toTimeString() methods to display only date and time parts of the Date object, respectively:

console.log(date.toDateString()); // Tue Sep 14 2021 console.log(date.toTimeString()); // 07:14:00 GMT+0500 (Pakistan Standard Time) 

You are not limited to the above methods only. Off course, you can also use methods like getDate() , getMonth() , and getFullYear() to retrieve day, month, and full year from a date object in JavaScript:

const day = date.getDate(); const month = date.getMonth() + 1; const year = date.getFullYear(); const str = `$day>/$month>/$year>`; console.log(str); // 14/9/2021 

The Intl.DateTimeFormat object is available in all modern browsers and IE 11. It provides methods for language-sensitive date and time formatting in JavaScript. One such method is format() that formats a date according to the locale and formatting options of the Intl.DateTimeFormat object. Here is an example that formats the date using the default locale:

const date = new Date(2021, 8, 14, 7, 14); const str = Intl.DateTimeFormat().format(date); console.log(str); // 14/9/2021 

If you need more localized date and time format, just pass the desired locale to Intl.DateTimeFormat() as shown below:

console.log(new Intl.DateTimeFormat('de-DE').format(date)); // 14.9.2021 console.log(new Intl.DateTimeFormat('ko-KR').format(date)); // 2021. 9. 14. console.log(new Intl.DateTimeFormat('ar-EG').format(date)); // ١٤‏/٩‏/٢٠٢١ 

The date object can be further customized by passing an options object to the Intl.DateTimeFormat() constructor:

const options =  weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' >; console.log(new Intl.DateTimeFormat('de-DE', options).format(date)); // Dienstag, 14. September 2021 options.timeZone = 'CET'; options.timeZoneName = 'short'; console.log(new Intl.DateTimeFormat('it-IT', options).format(date)); // martedì 14 settembre 2021, GMT+2 options.fractionalSecondDigits = 3; console.log(new Intl.DateTimeFormat('ar-EG', options).format(date)); // الاثنين، ١٣ سبتمبر ٢٠٢١ ٠٠٠ غرينتش-٥ 

You might also like.

Источник

Date.prototype.toISOString()

The toISOString() method of Date instances returns a string representing this date in the date time string format, a simplified format based on ISO 8601, which is always 24 or 27 characters long ( YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ , respectively). The timezone is always UTC, as denoted by the suffix Z .

Try it

Syntax

Return value

A string representing the given date in the date time string format according to universal time. It’s the same format as the one required to be recognized by Date.parse() .

Exceptions

Thrown if the date is invalid or if it corresponds to a year that cannot be represented in the date string format.

Examples

Using toISOString()

const d = new Date(0); console.log(d.toISOString()); // "1970-01-01T00:00:00.000Z" 

Specifications

Browser compatibility

BCD tables only load in the browser

See also

Found a content problem with this page?

This page was last modified on Jun 1, 2023 by MDN contributors.

Your blueprint for a better internet.

MDN

Support

Our communities

Developers

Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.

Источник

Оцените статью