Javascript formatting date to string format

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.

Источник

How to convert date to string dd/mm/yyyy format in Javascript and Node.js

There are many ways to convert date to string in Javascript. In this post, I will show you a few ways to format date in Javascript/Node.js to string in dd/mm/yyyy format with code example.

Using plain JavaScript #

These below examples will show you how to convert today date in Javascript to string in mm/dd/yyyy format.

var today = new Date(); var dd = String(today.getDate()).padStart(2, '0'); var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0! var yyyy = today.getFullYear(); today = mm + '/' + dd + '/' + yyyy; console.log(today); 

The output will be (the date of writing this post):

Of course you can change the order of month and day to your preference. For example, you can use the below code to convert date to string in dd/mm/yyyy format.

today = dd + '/' + mm + '/' + yyyy; console.log(today); 

The output will be (the date of writing this post):

If you prefer using hyphen instead of slash, you can use the below code to convert date to string in dd-mm-yyyy format.

today = dd + '-' + mm + '-' + yyyy; console.log(today); 

Ouput will be (the date of writing this post):

Tip: If you just need the date in yyyy-mm-dd format, you can use the below code to convert date to string in yyyy-mm-dd format.

let today = new Date() today = today.toISOString().split('T')[0] 

Output will be (the date of writing this post):

Using moment.js #

moment.js is a JavaScript library for parsing, validating, manipulating, and formatting dates and times. You can use it to convert date to string in dd/mm/yyyy format.

var today = moment().format('DD/MM/YYYY'); console.log(today); 

The output will be (the date of writing this post):

It is very easy to use moment.js to convert date to string in dd/mm/yyyy format. To change your date format, just change the format string in the format() method. Here is another example to convert date to string in yyyy-mm-dd format.

var today = moment().format('YYYY-MM-DD'); console.log(today); 

Using date-fns #

date-fns is modern JavaScript date utility library. date-fns provides the most comprehensive, yet simple and consistent toolset for manipulating JavaScript dates in a browser & Node.js. To get started with date-fns , please visit date-fns website.

It is very easy to use date-fns to convert date to string in dd/mm/yyyy format.

import  format > from 'date-fns' var day = format(new Date(2021, 11, 19), 'MM/dd/yyyy') //=> '11/19/2021' console.log(day); 

For more format string, please visit date-fns format.

Using day.js #

day.js is a fast 2kB alternative to Moment.js with the same modern API. To learn more about how to install and use day.js , please visit day.js website.

You can use day.js to convert date to string in dd/mm/yyyy format.

dayjs('2021-11-19').format('DD/MM/YYYY') // '19/11/2021' 

For more format string, please visit day.js format.

So those are 4 ways to format Javascript date to string in dd/mm/yyyy format or any other format you want. Use them to your advantage.

Источник

Читайте также:  Python ускорить регулярные выражения
Оцените статью