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

date - Split DateTime string in javascript - Stack Overflow

programmeradmin0浏览0评论

I have a Datetime string with unknown format, and I would like to split it to 2 different variables of Date & Time but keep the same format.

The format can contain several spaces so I can't split the string by spaces (for example: "MMMM dd, yyyy hh:mm:ss:SSS T").

How can I do it?

I have a Datetime string with unknown format, and I would like to split it to 2 different variables of Date & Time but keep the same format.

The format can contain several spaces so I can't split the string by spaces (for example: "MMMM dd, yyyy hh:mm:ss:SSS T").

How can I do it?

Share Improve this question asked Sep 9, 2014 at 6:46 user3530712user3530712 111 silver badge4 bronze badges 3
  • 1 what you have tried so far?? – Girish Commented Sep 9, 2014 at 6:47
  • Can you post something you have tried, what you exactly want to know? – softvar Commented Sep 9, 2014 at 6:49
  • Pure code-writing requests are off-topic on Stack Overflow -- we expect questions here to relate to specific programming problems -- but we will happily help you write it yourself! Tell us what you've tried, and where you are stuck. This will also help us answer your question better. – Cerbrus Commented Sep 9, 2014 at 6:52
Add a ment  | 

1 Answer 1

Reset to default 3

This could be a starting idea, but as @RobG noticed (in ment) is format dependent:

datetime = new Date(dateTimeInString);
day = datetime.getDate();
month = datetime.getMonth() + 1; //month: 0-11
year = datetime.getFullYear();
date = year + "-" + day + "-" + month;
hours = datetime.getHours();
minutes = datetime.getMinutes();
seconds = datetime.getSeconds();
time = hours + ":" + minutes + ":" + seconds;
发布评论

评论列表(0)

  1. 暂无评论