简体   繁体   English

如何将日期格式更改为等字符串

[英]how to change date format to isostring

I have array of object and I would like to change date format for both date.[date and dateofbirth]into iso string.我有 object 数组,我想将 date.[date 和 dateofbirth] 的日期格式更改为 iso 字符串。 I tried to use map to change date format but with no luck.我尝试使用 map 更改日期格式,但没有运气。 how can i change date format for both dates dynamically?如何动态更改两个日期的日期格式?

//array of object
const emp = [
{ name: "John", age: 22, date: "2015-03-25", dateofbirth: "1990-03-25" },
{ name: "Peter", age: 20, date: "2015-04-23", dateofbirth: "1389-03-03" },
{ name: "Mark", age: 23, date: "2015-01-20", dateofbirth: "1970-11-03" }
];

//my code 
let modifiedArr = emp.map(function (newDate) {
return newDate.date.toISOString;
});

console.log(modifiedArr);

Please read up on and get better understanding of the map() function from the documentation of map() function in JS .请从 JS 中的map() function 的文档中阅读并更好地理解 map map() function

The map() function iterates over the array and applies the specified callback on each item. map() function 遍历数组并对每个项目应用指定的回调。

Next, you need to construct Date objects using the Date constructor .接下来,您需要使用Date构造函数构造Date对象。 Only then can you call the toISOString() method.只有这样你才能调用toISOString()方法。

//array of object
const emp = [
  { name: "John", age: 22, date: "2015-03-25", dateofbirth: "1990-03-25" },
  { name: "Peter", age: 20, date: "2015-04-23", dateofbirth: "1389-03-03" },
  { name: "Mark", age: 23, date: "2015-01-20", dateofbirth: "1970-11-03" },
];

//my code
let modifiedArr = emp.map(function (empRecord) {
  return {
    ...empRecord,
    date: new Date(empRecord.date).toISOString(),
    dateofbirth: new Date(empRecord.dateofbirth).toISOString(),
  };
});

console.log(modifiedArr);

However, I strongly recommend luxon whenever dealing with dates.但是,无论何时处理日期,我都强烈推荐luxon Simply because the vanilla JS Date API is borked.仅仅因为香草 JS 日期 API 是无聊的。

 const emp = [ {name: "John", age: 22, date: "2015-03-25", dateofbirth: "1990-03-25"}, {name: "Peter", age: 20, date: "2015-04-23", dateofbirth: "1389-03-03"}, {name: "Mark", age: 23, date: "2015-01-20", dateofbirth: "1970-11-03"} ]; let dateKeys = ['date', 'dateofbirth']; var getDateInISO = function(date) { return new Date(date).toISOString(); } let modifiedArr = emp.map(function (elem) { dateKeys.forEach(function(date) { elem[date] = getDateInISO(elem[date]); }) return elem; }); console.log(modifiedArr);

toISOString() is method inside the javascript Date class, While the value of the date attribute is a string literal. toISOString()是 javascript日期class 中的方法,而日期属性的值是字符串文字。 Hence you will first have to get an instance of the Date class and then invoke the toISOString() function.因此,您首先必须获取日期 class 的实例,然后调用 toISOString() function。

This can be done in the following way,这可以通过以下方式完成,

let modifiedArr = emp.map(function (item) {

  // This will create a Date object representing the given date
  const currDate = new Date(item.date);

  return currDate.toISOString;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM