我在JSON数组中有完整的日期,但是根据我的需要,我只希望日期而不是月份和年份,就像完整日期是2018-12-01
一样,我只想要01
。
因为这是我的要求,所以我不知道如何将其连接起来
const raw = [
[
"JAYANAGAR",
"2018-09-01",
"476426"
],
[
"MALLESHWARAM",
"2018-09-01",
"92141"
],
[
"KOLAR",
"2018-09-01",
"115313"
],
[
"JAYANAGAR",
"2018-09-02",
"511153"
],
[
"MALLESHWARAM",
"2018-09-02",
"115704"
],
[
"KOLAR",
"2018-09-02",
"83597"
],
[
"JAYANAGAR",
"2018-09-03",
"167421"
],
[
"KOLAR",
"2018-09-03",
"53775"
]
]
let types = new Set();
const rawObj = raw.reduce((memo, [type, date, value]) => {
date = date.split('-').reverse().join('-');
memo[date] = memo[date] || {};
memo[date][type] = parseInt(value);
types.add(type);
return memo;
}, {});
types = [...types];
const data = Object.entries(rawObj).reduce((memo, [date, value]) => {
memo.push([date, ...types.map(type => value[type] || 0)]);
return memo;
}, [
['Billdate', ...types.map(type => `${type[0]}${type.substr(1).toLowerCase()}`)]
]);
console.log(data)
在我的代码中,我正在格式化我的JSON数据,因为我现在只想要日期而不是月份和年份。
任何人请我指导或建议我如何实现这一目标
答案 0 :(得分:3)
var date = new Date('2019-01-02');
alert(date.getDate())
.getDate()
是从日期返回日期的函数。
如果日期是字符串 做-
console.log(date.slice(-2));
答案 1 :(得分:2)
您可以先将旅行日期字符串转换为日期对象,然后使用getDate()
获取日期。这将保证支持各种日期时间格式。
var date = new Date('2018-09-01')
console.log(date.getDate());
根据您的数组raw
数据,假设您的日期始终为1索引,则可以尝试
const raw = [
[
"JAYANAGAR",
"2018-09-01",
"476426"
],
[
"MALLESHWARAM",
"2018-09-01",
"92141"
],
[
"KOLAR",
"2018-09-01",
"115313"
],
[
"JAYANAGAR",
"2018-09-02",
"511153"
],
[
"MALLESHWARAM",
"2018-09-02",
"115704"
],
[
"KOLAR",
"2018-09-02",
"83597"
],
[
"JAYANAGAR",
"2018-09-03",
"167421"
],
[
"KOLAR",
"2018-09-03",
"53775"
]
]
var dateList = raw.map(data => {
return new Date(data[1]).getDate();
})
console.log(dateList)
答案 2 :(得分:1)
您可以将日期字符串更改为Date对象,并使用getDate函数获取日期部分
var d = new Date('2018-09-03');
console.log(d.getDate())
答案 3 :(得分:0)
尝试添加此内容:
const parent = this.props.navigation.dangerouslyGetParent();
const isDrawerOpen = parent && parent.state && parent.state.isDrawerOpen;
答案 4 :(得分:0)
您可以通过一点正则表达式魔术来完成此操作:
var regex = /^\d{4}-\d\d-(\d\d)$/;
var dates = []
for (let i = 0; i < raw.length; i++) {
var match = regex.exec(raw[i][1]);
dates.push(match[1])
}
正则表达式匹配您的日期字符串(4位数字“-” 2位数字“-” 2位数字),并在将日期推送到数组之前捕获日期。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
答案 5 :(得分:0)
我将告诉您在第一条语句中以所需的形式获取原始数组,如下所示。
注意:然后在此之后继续前进。使用
new Date()
语句创建新日期没有意义。
const raw = [
[
"JAYANAGAR",
"2018-09-01",
"476426"
],
[
"MALLESHWARAM",
"2018-09-01",
"92141"
],
[
"KOLAR",
"2018-09-01",
"115313"
],
[
"JAYANAGAR",
"2018-09-02",
"511153"
],
[
"MALLESHWARAM",
"2018-09-02",
"115704"
],
[
"KOLAR",
"2018-09-02",
"83597"
],
[
"JAYANAGAR",
"2018-09-03",
"167421"
],
[
"KOLAR",
"2018-09-03",
"53775"
]
].map((l) => { l[1] = l[1].split('-')[2]; return l})
console.log(raw)
/*
[ [ 'JAYANAGAR', '01', '476426' ],
[ 'MALLESHWARAM', '01', '92141' ],
[ 'KOLAR', '01', '115313' ],
[ 'JAYANAGAR', '02', '511153' ],
[ 'MALLESHWARAM', '02', '115704' ],
[ 'KOLAR', '02', '83597' ],
[ 'JAYANAGAR', '03', '167421' ],
[ 'KOLAR', '03', '53775' ] ]
*/
答案 6 :(得分:0)
尝试此代码
var weekday = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var d = new Date('08/11/2015');
alert(weekday[d.getDay()]);
答案 7 :(得分:0)
您可以使用简单易用的moment.js:
<script src="https://momentjs.com/downloads/moment.js"/>
var day = moment(new Date('2018-09-03')).format('DD');