Javascript格式化日期数组

时间:2016-09-03 02:22:30

标签: javascript jquery

我有这个阵列......

var dates = [{"date_play":"2016-08-22 00:00:00"},{"date_play":"2016-08-23 00:00:00"},{"date_play":"2016-08-24 00:00:00"}];

如何格式化该日期以生成如下:

var newDates = ['08-22-2016','08-23-2016','08-24-2016'];

3 个答案:

答案 0 :(得分:2)

鉴于您已有文字,您可以操纵文字。



var dates = [{"date_play":"2016-08-22 00:00:00"},{"date_play":"2016-08-23 00:00:00"},{"date_play":"2016-08-24 00:00:00"}];

var newDates = [];
$.each(dates,function(i,obj){
    newDates.push(obj['date_play'].replace(/(\d{4})-(\d\d)-(\d\d).*$/,'$2-$3-$1'));
});
console.log(newDates);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

或者:

&#13;
&#13;
var dates = [{"date_play":"2016-08-22 00:00:00"},{"date_play":"2016-08-23 00:00:00"},{"date_play":"2016-08-24 00:00:00"}];

var newDates = [];
dates.forEach(function(obj){
    newDates.push(obj['date_play'].replace(/(\d{4})-(\d\d)-(\d\d).*$/,'$2-$3-$1'));
});
console.log(newDates);
&#13;
&#13;
&#13;

或者:

&#13;
&#13;
var dates = [{"date_play":"2016-08-22 00:00:00"},{"date_play":"2016-08-23 00:00:00"},{"date_play":"2016-08-24 00:00:00"}];

var newDates = dates.map(function(obj){
    return obj['date_play'].replace(/(\d{4})-(\d\d)-(\d\d).*$/,'$2-$3-$1');
});
console.log(newDates);
&#13;
&#13;
&#13;

或者:

&#13;
&#13;
var dates = [{"date_play":"2016-08-22 00:00:00"},{"date_play":"2016-08-23 00:00:00"},{"date_play":"2016-08-24 00:00:00"}];

var newDates = dates.map(function(obj){
    var tmp = obj['date_play'].split(/[- ]/,3);
    return tmp[1] + '-' + tmp[2] + '-' + tmp[0];
});
console.log(newDates);
&#13;
&#13;
&#13;

解释replace()正则表达式:
在这种情况下,replace()RegExp(正则表达式)一起使用,它使用三个capture groups来暂时保存月,日和年,然后用刚刚替换字符串捕获组的内容由-个字符分隔,但顺序不同(月 - 日 - 年)。

replace(/(\d{4})-(\d\d)-(\d\d).*$/,'$2-$3-$1')
         │ ││ ││ │ │ ││ │ │ │││││    │  │  └───Replace with capture group #1 (year)
         │ │┕┯┙│ │ │ ││ │ │ │││││    │  └──────Replace with capture group #3 (day)
         │ │ │ │ │ │ ││ │ │ │││││    └─────────Replace with capture group #2 (month)
         │ │ │ │ │ │ ││ │ │ ││││└────────────── $   = The end of the string (not really needed here)
         │ │ │ │ │ │ ││ │ │ │││└─────────────── *   = Repeat the previous character, or group, 0 up to as many times as possible ( .* matches the rest of the string)
         │ │ │ │ │ │ ││ │ │ ││└──────────────── .   = Any character
         │ │ │ │ │ │ ││ │ │ │└───────────────── )   = End capture group #3 (which is 2 digits, the day)
         │ │ │ │ │ │ ││ │ │ └────────────────── \d  = A digit
         │ │ │ │ │ │ ││ │ └──────────────────── \d  = A digit
         │ │ │ │ │ │ ││ └────────────────────── (   = Begin capture group #3
         │ │ │ │ │ │ │└──────────────────────── )   = End capture group #2 (which is 2 digits, the month)
         │ │ │ │ │ │ └───────────────────────── \d  = A digit
         │ │ │ │ │ └─────────────────────────── \d  = A digit
         │ │ │ │ └───────────────────────────── (   = Begin capture group #3
         │ │ │ └─────────────────────────────── )   = End capture group #1 (which is 4 digits, the year)
         │ │ └───────────────────────────────── {4} = Repeat the previous character, or group, exactly 4 times (4 digits, the year)
         │ └─────────────────────────────────── \d  = A digit
         └───────────────────────────────────── (   = Begin capture group #1

All of the '-' characters are just '-' characters: part of the old string, or part
of the replacement.

答案 1 :(得分:1)

试试这个:

var newDates = [];
    $.each(data, function(i, item) {
       var date = new Date(item.date_play);
       newDates.push((date.getMonth() + 1) + '/' + date.getDate() + '/' +  date.getFullYear());
    }) 

答案 2 :(得分:1)

您不需要jQuery或Date解析器;你所需要的只是分解字符串。

重要的是不要将它转换为日期对象,因为当你这样做时,它会强制执行时区,并且该对象甚至可能歪曲你的一天(例如,2016-08-22可能变成8-21-2016)时区转移。

let dates = [
  {"date_play":"2016-08-22 00:00:00"},
  {"date_play":"2016-08-23 00:00:00"},
  {"date_play":"2016-08-24 00:00:00"}
];
let newDates = dates.map(obj=>{
  let dt = obj.date_play.split(' ')[0].split('-');
  return [dt[1],dt[2],dt[0]].join('-');
});

因为看起来您的数据输入是统一的,所以要么调整来源,要么重新排列字符串内容。