只有月份和年份的 Unix 时间戳
Unix timestamp with only month and year
我有一组不同的日期,但它们只包括月份和年份。有没有办法从这两件事中获取 unix 时间戳?我必须遍历数组以获取所有时间戳。我有解决方案,但解决方案又丑又长。有没有 simpler/easier 方法来减少我的代码量?
for(var i=0, len = arr.length; i < len; i++){
arr[i]['newDate'] = [];
var jan = day * 31;
var year = 31556926;
var day = 86400;
if(arr[i].post_date.includes("Janurary")){
//january
var str = arr[i].post_date.substr(8);
var current = parseInt(str);
var yearTotal = (current - 1970) * year;
var unix = jan + yearTotal;
var dateString = "@" + unix.toString();
arr[i].newDate.push(dateString);
}
if(arr[i].post_date.includes("February")){
//february
var str = arr[i].post_date.substr(8);
var current = parseInt(str);
var yearTotal = (current - 1970) * year;
var unix = feb + yearTotal;
var dateString = "@" + unix.toString();
arr[i].newDate.push(dateString);
}
if(arr[i].post_date.includes("March")){
//march
var str = arr[i].post_date.substr(6);
var current = parseInt(str);
var yearTotal = (current - 1970) * year;
var unix = mar + yearTotal;
var dateString = "@" + unix.toString();
arr[i].newDate.push(dateString);
}
if(arr[i].post_date.includes("April")){
//april
var str = arr[i].post_date.substr(6);
var current = parseInt(str);
var yearTotal = (current - 1970) * year;
var unix = apr + yearTotal;
var dateString = "@" + unix.toString();
arr[i].newDate.push(dateString);
}
if(arr[i].post_date.includes("May")){
//may
var str = arr[i].post_date.substr(4);
var current = parseInt(str);
var yearTotal = (current - 1970) * year;
var unix = may + yearTotal;
var dateString = "@" + unix.toString();
arr[i].newDate.push(dateString);
}
if(arr[i].post_date.includes("June")){
//june
var str = arr[i].post_date.substr(5);
var current = parseInt(str);
var yearTotal = (current - 1970) * year;
var unix = jun + yearTotal;
var dateString = "@" + unix.toString();
arr[i].newDate.push(dateString);
}
if(arr[i].post_date.includes("July")){
//july
var str = arr[i].post_date.substr(5);
var current = parseInt(str);
var yearTotal = (current - 1970) * year;
var unix = jul + yearTotal;
var dateString = "@" + unix.toString();
arr[i].newDate.push(dateString);
}
if(arr[i].post_date.includes("August")){
//august
var str = arr[i].post_date.substr(7);
var current = parseInt(str);
var yearTotal = (current - 1970) * year;
var unix = aug + yearTotal;
var dateString = "@" + unix.toString();
arr[i].newDate.push(dateString);
}
if(arr[i].post_date.includes("September")){
//september
var str = arr[i].post_date.substr(10);
var current = parseInt(str);
var yearTotal = (current - 1970) * year;
var unix = sep + yearTotal;
var dateString = "@" + unix.toString();
arr[i].newDate.push(dateString);
}
if(arr[i].post_date.includes("October")){
//october
var str = arr[i].post_date.substr(8);
var current = parseInt(str);
var yearTotal = (current - 1970) * year;
var unix = oct + yearTotal;
var dateString = "@" + unix.toString();
arr[i].newDate.push(dateString);
}
if(arr[i].post_date.includes("November")){
//november
var str = arr[i].post_date.substr(9);
var current = parseInt(str);
var yearTotal = (current - 1970) * year;
var unix = nov + yearTotal;
var dateString = "@" + unix.toString();
arr[i].newDate.push(dateString);
}
if(arr[i].post_date.includes("December")){
//december
var str = arr[i].post_date.substr(9);
var current = parseInt(str);
var yearTotal = (current - 1970) * year;
var unix = dec + yearTotal;
var dateString = "@" + unix.toString();
arr[i].newDate.push(dateString);
}
var jan = day * 31;
var feb = day * 28;
var mar = day * 31;
var apr = day * 30;
var may = day * 31;
var jun = day * 30;
var jul = day * 31;
var aug = day * 31;
var sep = day * 30;
var oct = day * 31;
var nov = day * 30;
var dec = day * 31;
}
我的数组看起来像这样
var arr = [
{
"post_title": "title",
"post_date": "December 2021"
},
{
"post_title": "title",
"post_date": "November 2021"
},
{
"post_title": "title",
"post_date": "October 2021"
},
{
"post_title": "title",
"post_date": "September 2021"
},
{
"post_title": "title",
"post_date": "August 2021"
},
{
"post_title": "title",
"post_date": "July 2021"
},
{
"post_title": "title",
"post_date": "June 2021"
}
]
我的结果看起来像
var arr = [
{
"post_title": "title",
"post_date": "December 2021",
"newDate": [
"@1612081626"
]
},
{
"post_title": "title",
"post_date": "November 2021",
"newDate": [
"@1611995226"
]
},
{
"post_title": "title",
"post_date": "October 2021",
"newDate": [
"@1612081626"
]
},
{
"post_title": "title",
"post_date": "September 2021",
"newDate": [
"@1611995226"
]
},
{
"post_title": "title",
"post_date": "August 2021",
"newDate": [
"@1612081626"
]
},
{
"post_title": "title",
"post_date": "July 2021",
"newDate": [
"@1612081626"
]
},
{
"post_title": "title",
"post_date": "June 2021",
"newDate": [
"@1611995226"
]
}
]
您可以使用字符串中的月份(转换为索引)和年份简单地创建 new Date()
。
此处使用月份数组将命名月份转换为索引,并将结果除以 1000,因为 javascript 以毫秒为单位存储时间,而 unix 使用秒。
const arr = [{ post_title: 'title', post_date: 'December 2021', }, { post_title: 'title', post_date: 'November 2021', }, { post_title: 'title', post_date: 'October 2021', }, { post_title: 'title', post_date: 'September 2021', }, { post_title: 'title', post_date: 'August 2021', }, { post_title: 'title', post_date: 'July 2021', }, { post_title: 'title', post_date: 'June 2021', },];
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December',];
const result = arr.map((o) => {
const [month, year] = o.post_date.split(' ');
return {
...o,
unix_timestamp: new Date(year, months.indexOf(month)).valueOf() / 1000,
};
});
console.log(result);
注意:月份是 0 索引,从一月的 0 到十二月的 11
你真的不需要查找 table:
const arr = [{ post_title: 'title', post_date: 'December 2021', }, { post_title: 'title', post_date: 'November 2021', }, { post_title: 'title', post_date: 'October 2021', }, { post_title: 'title', post_date: 'September 2021', }, { post_title: 'title', post_date: 'August 2021', }, { post_title: 'title', post_date: 'July 2021', }, { post_title: 'title', post_date: 'June 2021', },];
const result = arr.map(d =>
({...d,
unix_timestamp: new Date(d.post_date.replace(" "," 1. ")+" 00:00:00").valueOf() / 1000}));
console.log(result);
我有一组不同的日期,但它们只包括月份和年份。有没有办法从这两件事中获取 unix 时间戳?我必须遍历数组以获取所有时间戳。我有解决方案,但解决方案又丑又长。有没有 simpler/easier 方法来减少我的代码量?
for(var i=0, len = arr.length; i < len; i++){
arr[i]['newDate'] = [];
var jan = day * 31;
var year = 31556926;
var day = 86400;
if(arr[i].post_date.includes("Janurary")){
//january
var str = arr[i].post_date.substr(8);
var current = parseInt(str);
var yearTotal = (current - 1970) * year;
var unix = jan + yearTotal;
var dateString = "@" + unix.toString();
arr[i].newDate.push(dateString);
}
if(arr[i].post_date.includes("February")){
//february
var str = arr[i].post_date.substr(8);
var current = parseInt(str);
var yearTotal = (current - 1970) * year;
var unix = feb + yearTotal;
var dateString = "@" + unix.toString();
arr[i].newDate.push(dateString);
}
if(arr[i].post_date.includes("March")){
//march
var str = arr[i].post_date.substr(6);
var current = parseInt(str);
var yearTotal = (current - 1970) * year;
var unix = mar + yearTotal;
var dateString = "@" + unix.toString();
arr[i].newDate.push(dateString);
}
if(arr[i].post_date.includes("April")){
//april
var str = arr[i].post_date.substr(6);
var current = parseInt(str);
var yearTotal = (current - 1970) * year;
var unix = apr + yearTotal;
var dateString = "@" + unix.toString();
arr[i].newDate.push(dateString);
}
if(arr[i].post_date.includes("May")){
//may
var str = arr[i].post_date.substr(4);
var current = parseInt(str);
var yearTotal = (current - 1970) * year;
var unix = may + yearTotal;
var dateString = "@" + unix.toString();
arr[i].newDate.push(dateString);
}
if(arr[i].post_date.includes("June")){
//june
var str = arr[i].post_date.substr(5);
var current = parseInt(str);
var yearTotal = (current - 1970) * year;
var unix = jun + yearTotal;
var dateString = "@" + unix.toString();
arr[i].newDate.push(dateString);
}
if(arr[i].post_date.includes("July")){
//july
var str = arr[i].post_date.substr(5);
var current = parseInt(str);
var yearTotal = (current - 1970) * year;
var unix = jul + yearTotal;
var dateString = "@" + unix.toString();
arr[i].newDate.push(dateString);
}
if(arr[i].post_date.includes("August")){
//august
var str = arr[i].post_date.substr(7);
var current = parseInt(str);
var yearTotal = (current - 1970) * year;
var unix = aug + yearTotal;
var dateString = "@" + unix.toString();
arr[i].newDate.push(dateString);
}
if(arr[i].post_date.includes("September")){
//september
var str = arr[i].post_date.substr(10);
var current = parseInt(str);
var yearTotal = (current - 1970) * year;
var unix = sep + yearTotal;
var dateString = "@" + unix.toString();
arr[i].newDate.push(dateString);
}
if(arr[i].post_date.includes("October")){
//october
var str = arr[i].post_date.substr(8);
var current = parseInt(str);
var yearTotal = (current - 1970) * year;
var unix = oct + yearTotal;
var dateString = "@" + unix.toString();
arr[i].newDate.push(dateString);
}
if(arr[i].post_date.includes("November")){
//november
var str = arr[i].post_date.substr(9);
var current = parseInt(str);
var yearTotal = (current - 1970) * year;
var unix = nov + yearTotal;
var dateString = "@" + unix.toString();
arr[i].newDate.push(dateString);
}
if(arr[i].post_date.includes("December")){
//december
var str = arr[i].post_date.substr(9);
var current = parseInt(str);
var yearTotal = (current - 1970) * year;
var unix = dec + yearTotal;
var dateString = "@" + unix.toString();
arr[i].newDate.push(dateString);
}
var jan = day * 31;
var feb = day * 28;
var mar = day * 31;
var apr = day * 30;
var may = day * 31;
var jun = day * 30;
var jul = day * 31;
var aug = day * 31;
var sep = day * 30;
var oct = day * 31;
var nov = day * 30;
var dec = day * 31;
}
我的数组看起来像这样
var arr = [
{
"post_title": "title",
"post_date": "December 2021"
},
{
"post_title": "title",
"post_date": "November 2021"
},
{
"post_title": "title",
"post_date": "October 2021"
},
{
"post_title": "title",
"post_date": "September 2021"
},
{
"post_title": "title",
"post_date": "August 2021"
},
{
"post_title": "title",
"post_date": "July 2021"
},
{
"post_title": "title",
"post_date": "June 2021"
}
]
我的结果看起来像
var arr = [
{
"post_title": "title",
"post_date": "December 2021",
"newDate": [
"@1612081626"
]
},
{
"post_title": "title",
"post_date": "November 2021",
"newDate": [
"@1611995226"
]
},
{
"post_title": "title",
"post_date": "October 2021",
"newDate": [
"@1612081626"
]
},
{
"post_title": "title",
"post_date": "September 2021",
"newDate": [
"@1611995226"
]
},
{
"post_title": "title",
"post_date": "August 2021",
"newDate": [
"@1612081626"
]
},
{
"post_title": "title",
"post_date": "July 2021",
"newDate": [
"@1612081626"
]
},
{
"post_title": "title",
"post_date": "June 2021",
"newDate": [
"@1611995226"
]
}
]
您可以使用字符串中的月份(转换为索引)和年份简单地创建 new Date()
。
此处使用月份数组将命名月份转换为索引,并将结果除以 1000,因为 javascript 以毫秒为单位存储时间,而 unix 使用秒。
const arr = [{ post_title: 'title', post_date: 'December 2021', }, { post_title: 'title', post_date: 'November 2021', }, { post_title: 'title', post_date: 'October 2021', }, { post_title: 'title', post_date: 'September 2021', }, { post_title: 'title', post_date: 'August 2021', }, { post_title: 'title', post_date: 'July 2021', }, { post_title: 'title', post_date: 'June 2021', },];
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December',];
const result = arr.map((o) => {
const [month, year] = o.post_date.split(' ');
return {
...o,
unix_timestamp: new Date(year, months.indexOf(month)).valueOf() / 1000,
};
});
console.log(result);
注意:月份是 0 索引,从一月的 0 到十二月的 11
你真的不需要查找 table:
const arr = [{ post_title: 'title', post_date: 'December 2021', }, { post_title: 'title', post_date: 'November 2021', }, { post_title: 'title', post_date: 'October 2021', }, { post_title: 'title', post_date: 'September 2021', }, { post_title: 'title', post_date: 'August 2021', }, { post_title: 'title', post_date: 'July 2021', }, { post_title: 'title', post_date: 'June 2021', },];
const result = arr.map(d =>
({...d,
unix_timestamp: new Date(d.post_date.replace(" "," 1. ")+" 00:00:00").valueOf() / 1000}));
console.log(result);