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
Add a ment  | 

5 Answers 5

Reset to default 1

If 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 getMonthis 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