{

How to Convert string to/from Date in javascript?| examples


This tutorial is about converting string to/from Date in javascript with examples. These examples work in typescript as typescript is a superset of javascript.

String and Date types in typescript

string is an object that holds the sequence of characters enclosed in double-quotes. Date objects have default current date and time in typescript. Date with default constructor returns current date-time. The date object holds different timezones - GMT and UTC.

String format should be ISO format. ISO format is of YYYY-MM-DDTHH:mm:SS and YYYY-MM-DD.

if you want a timezone like UTC format, suffix z.

String and date are objects in typescript which hold different values.

Sometimes, want to manipulate dates and convert to/from different types. We have a momentJS library to handle and manipulate Date objects using typescript and javascript.

How to Convert String to Date in typescript?

We have many ways to convert to String to date in typescript

Using Date constructor

It is one of the simple and easy ways to convert to a Date object. Pass string date value to Date constructor.

Syntax

new Date(String);

Return a valid date if a string is in ‘ISO format.’ And returns an invalid date if a string date is not in ISO format.

Here is an example

let dateString = '2015-10-06'   
let dateString1= '10-06-2015'   
let dateString2= 'asdfasdfasdfads'  
let newDate = new Date(dateString);  
let newDate1 = new Date(dateString1);  
let newDate2 = new Date(dateString2);  
console.log(newDate) // Tue Oct 06 2015 05:30:00 GMT+0530 (India Standard Time)  
console.log(newDate1) // Tue Oct 06 2015 00:00:00 GMT+0530 (India Standard Time)  
console.log(newDate2)//  Invalid Date  

momentjs to convert to Date from String

momentjs library provides the below constructor

  moment(stringDate,formatString)

Arguments: stringDate - valid date in string formatString - Output Date format

string contains date data in the format of MM-DD-YYYY then we have to use the method for converting to a Date object.

  
var dateString = '08-25-2018';  
var momentVariable = moment(dateString, 'MM-DD-YYYY');  
var stringvalue = momentVariable.format('YYYY-MM-DD');   
console.log(stringvalue); // outputs 2018-08-25  

How to convert Date to String in typescript

We have many ways to achieve this using the toDateString() method and moment library. We will see both below with examples.

Using toDateString() method

Date object one method called toDateString() that returns a date in the form of a string.

Here is an example of Converting from Date to String

var date = new Date('2017-03-07T10:00:00');  
let str = date.toDateString();  
console.log(str) //Tue Mar 07 2017  
console.log(typeof str) // string  

Using momentjs example

Date object is created, passing this date object to moment constructor and called format method that parses date as per the format described.

var date = new Date('2017-03-07T10:00:00');  
var formatted = moment(date).format('D MMMM YYYY');  
console.log('formatted' ,formatted) // outputs 7 March 2017  
console.log('formatted' ,typeof formatted) // outputs string  

ES6 toLocaleDateString toLocaleTimeString methods

ES6 introduced toLocaleDateString and toLocaleTimeString to Date object.

The toLocaleDateString method converts the current date with localized format into a String.

const date = new Date();

const localDateString = date.toLocaleDateString('En-en');
console.log(localDateString); //6/18/2021
console.log(type localDateString); // string

The toLocaleTimeString method converts current time with localized format into String.

const date = new Date();
const localDateString = date.toLocaleTimeString('En-en');

console.log(localDateString); //8:50:29 PM
console.log(type localDateString); // string

Conclusion

To sum up, We learned multiple ways to convert String to Date and Date to String in Javascript and typescript.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.