2

How can I convert 201604130630 this numbers to date time format in JavaScript.

I am using XML file and this data coming from XML. I want to convert into date format. Please help.

3
  • You split and concatenate into the format you'd like Commented Apr 13, 2016 at 3:15
  • how do you like it to be formatted? 2016/04/13 06:30 ? Commented Apr 13, 2016 at 3:16
  • i want this 2016/04/13 06:30 format Commented Apr 13, 2016 at 3:21

3 Answers 3

1
var str = "201604130630";
var year = str.slice(0, 4);
var month = parseInt(str.slice(4, 6))-1;
var day = str.slice(6, 8);
var hour = str.slice(8, 10);
var minute = str.slice(10, 12);
var d = new Date(year, month, day, hour, minute)
alert(d);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you ghazi2008..It worked for me.Thank you so much
0

You can create a date from the string by splitting the string

var input = '201604130630';
var year = input.slice(0, 4);
// Date object uses month 0 index based
var month = parseInt(input.slice(4, 6)) + 1;
var day = input.slice(6, 8);
var hour = input.slice(8, 10);
var minute = input.slice(10, 12);

var d = new Date(year, month, day, hour, minute);

And then you can just concatenate the values or format it using Date methods:

Comments

0
var d = "201604130630";
var s = d.substring(0, 4) + '/' + d.substring(4, 6) + '/' + d.substring(6, 8) + ' ' + d.substring(8, 10) + ':' + d.substring(10, 12);
alert(s);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.