Skip to content Skip to sidebar Skip to footer

Convert Date String(yymmdd) To Date Object In JS

I have a date string in 'yymmdd' format i want to convert it into date object in JS the input string is '161208' I have tried code below var mydate = new Date(InputDate); but it s

Solution 1:

Check my answer. Basically you first need to give a proper format to your string. You can either do it manually or use moment.js.

stringFormat = moment(dateObject).format("YYYY-MM-DDTHH:mm:ss");
date = new Date(stringFormat);

This is also really helpful to understand how the different string formats are compatible with different browsers.


Solution 2:

I'm not sure if this is what you're after?

var s = '161208';
var dt = new Date('20' + s.substring(0, 2) + '-' + s.substring(2, 4) + '-' + s.substring(4));

Post a Comment for "Convert Date String(yymmdd) To Date Object In JS"