admin管理员组文章数量:1431955
var d ={
"title": "q",
"message": "q",
"date_time": "March 28, 2018 7:12 PM",
"user_id": "5abb6c929d5b611b01875e9b"
}
var s = d.date_time.split(',')
console.log(s)
var d ={
"title": "q",
"message": "q",
"date_time": "March 28, 2018 7:12 PM",
"user_id": "5abb6c929d5b611b01875e9b"
}
var s = d.date_time.split(',')
console.log(s)
Share
Improve this question
edited May 3, 2018 at 20:27
Basil Bourque
342k124 gold badges936 silver badges1.3k bronze badges
asked May 3, 2018 at 12:10
Rajesh Kumar JRajesh Kumar J
2291 gold badge5 silver badges8 bronze badges
2
-
Use the normal Date object:
var datetime = new Date(d.date_time);
you can split whatever you need from there. Also Momentjs is very easy to use and can do a lot with dates. – Mike Bovenlander Commented May 3, 2018 at 12:14 - Take care when spelling JavaScript. JavaScriptIsNotJava.io – Basil Bourque Commented May 3, 2018 at 20:28
5 Answers
Reset to default 1If you don't want to add a library like moment.js
, I'd suggest creating a native JavaScript Date
object and working with that to extract whatever part you need from it.
const date = new Date(d.date_time);
console.log(`${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`);
console.log(`${date.getHours()}:${date.getMinutes()}`);
A couple of things to note:
- The return value of
getMonth
is zero-based - You'll have to use your own logic to convert 24-hour to 12-hour AM/PM time if that's required
- Beware of time zones
You can use the js plugin moment.js:
var dateStr = moment(d.date_time).format('LL');
var timeStr = moment(d.date_time).format('LT');
See here for info: https://momentjs.
If you need an array then just do this: var cars = [dateStr, timeStr];
You can use regular expression
myArray = /(\w+ \d+), (\d+) (\d+):(\d+) (\w+)/g.exec('March 28, 2018 7:12 PM');
You will get array of
0:"March 28, 2018 7:12 PM"
1:"March 28"
2:"2018"
3:"7"
4:"12"
5:"PM"
You can work with that.
Or if you need to just split date and time use:
myArray = /(\w+ \d+, \d+) (\d+:\d+ \w+)/g.exec('March 28, 2018 7:12 PM');
Your result will be:
0:"March 28, 2018 7:12 PM"
1:"March 28, 2018"
2:"7:12 PM"
var d ={
"title": "q",
"message": "q",
"date_time": "March 28, 2018 7:12 PM",
"user_id": "5abb6c929d5b611b01875e9b"
};
var data = d.date_time.split(' ');
var date = data[0]+' '+data[1]+' '+data[2];
var time = data[3]+' '+data[4];
console.log(date);
console.log(time);
Split the string with space , then get the data.
var d ={
"title": "q",
"message": "q",
"date_time": "March 28, 2018 7:12 PM",
"user_id": "5abb6c929d5b611b01875e9b"
},
new_date_time = new Date( d.date_time ),
s = new_date_time.toLocaleDateString(),
t = new_date_time.toLocaleTimeString();
console.log('Date: ' + s );
console.log('Time: ' + t )
本文标签: how to split date and time from the datetime variable in JavaScriptStack Overflow
版权声明:本文标题:how to split date and time from the datetime variable in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745588304a2665044.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论