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
1 Answer
Reset to default 3This 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;