1

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 says invalid date. Here is the fiddle https://jsfiddle.net/4ry0hL4t/

the basic need is that, I have a date string in "yymmdd" format and i have to convert it to different date formats like ("yyyy/mm/dd, yy-mm-dd","yy/mm").

AddyProg
  • 2,960
  • 13
  • 59
  • 110
  • 4
    Try [momentjs](http://momentjs.com/) library, that's a pretty good library for parsing. Javascripts and dates was never really such a good match (eg: January is the 0 month, not 1st ...) – Icepickle Dec 13 '16 at 13:40
  • Maybe this post can help you further. http://stackoverflow.com/questions/10607935/convert-returned-string-yyyymmdd-to-date – gurtjun Dec 13 '16 at 13:41

2 Answers2

0

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.

Community
  • 1
  • 1
Andres Elizondo
  • 331
  • 3
  • 15
-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));
codemonkey65
  • 120
  • 1
  • 6