将日期格式从JSON更改为前2个字母和年份

时间:2017-03-30 16:27:01

标签: javascript jquery json

我有以下JSON,我需要转换日期值,例如。 " 2016年12月"对于" D16",不知道我怎么能做到这一点,我想有一个正则表达式,任何帮助将不胜感激。

[["November 2016","December 2016","January 2017","February 2017","March 2017"],["total",2,43,59,51,41
],["mammo",1,20,33,12,12],["face",1,20,16,30,27],["body",0,3,10,9,2],["photo",0,19,27,12,5],["scan",2
,24,32,39,36]]

3 个答案:

答案 0 :(得分:1)

var dates = ["November 2016","December 2016","January 2017","February 2017","March 2017"];

dates = dates.map((date) => date[0] + date.split(' ')[1].slice(2, 4))

console.log(dates)
//["N16", "D16", "J17", "F17", "M17"]

如果你需要用日期做一些其他的魔法,你可以使用一些类似的时刻。

答案 1 :(得分:0)

要实现此目的,您可以使用正则表达式匹配所需的文本并替换第一个日期数组的元素。试试这个:



var arr = [
  ["November 2016", "December 2016", "January 2017", "February 2017", "March 2017"],
  ["total", 2, 43, 59, 51, 41],
  ["mammo", 1, 20, 33, 12, 12],
  ["face", 1, 20, 16, 30, 27],
  ["body", 0, 3, 10, 9, 2],
  ["photo", 0, 19, 27, 12, 5],
  ["scan", 2, 24, 32, 39, 36]
];

arr[0] = arr[0].map(function(date) {
  return date.replace(/^(\w{1}).*(\d{2})$/gi, "$1$2");
})

console.log(arr);

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

答案 2 :(得分:0)

这应该有效

os.path.join