最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

datetime - JavaScript - Convert date stirng yyyyMMddHHmmss to date object yyyy-MM-dd HH:mm:ss - Stack Overflow

programmeradmin3浏览0评论

I have this date : 2014071109080706ICT

I need to convert it to Date object in JS I tried to create new object new Date("2014071109080706ICT") but I get error Invalid date I also tried cut date string to "20140711090807" and create new Date object but it always generate error : Invalid date How can i do it ?

I have this date : 2014071109080706ICT

I need to convert it to Date object in JS I tried to create new object new Date("2014071109080706ICT") but I get error Invalid date I also tried cut date string to "20140711090807" and create new Date object but it always generate error : Invalid date How can i do it ?

Share Improve this question asked Jul 11, 2014 at 3:01 RyoRyo 1,0353 gold badges26 silver badges45 bronze badges 2
  • developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Derek 朕會功夫 Commented Jul 11, 2014 at 3:02
  • try new Date(2014071109080706) – gp. Commented Jul 11, 2014 at 3:05
Add a ment  | 

2 Answers 2

Reset to default 4

You can try to use moment.js . http://momentjs.

There are some examples in docs page. One of them is:

moment("2010-10-20 4:30 +0000", "YYYY-MM-DD HH:mm Z");

http://momentjs./docs/#/parsing/

You can try:

moment("20140711090807+0600", "YYYYMMDDHHmmssZZ"); I think "06ICT" is the timezone info.

You just need to slice the string for each segment and create the date object based on those parts.

var str = "20140711090807";
var year = str.substring(0, 4);
var month = str.substring(4, 6);
var day = str.substring(6, 8);
var hour = str.substring(8, 10);
var minute = str.substring(10, 12);
var second = str.substring(12, 14);
var date = new Date(year, month-1, day, hour, minute, second);

PS: month index is between 0 and 11 so you need to subtract it by 1.

发布评论

评论列表(0)

  1. 暂无评论