I have full Date in my JSON Array,but as of my needs i only want the date not the month and year like if full date is 2018-12-01
i want to have only 01
.
As this is the requirement for me and i have no idea how can i concatenate this
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)
I have full Date in my JSON Array,but as of my needs i only want the date not the month and year like if full date is 2018-12-01
i want to have only 01
.
As this is the requirement for me and i have no idea how can i concatenate this
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)
here in my code i am formatting my JSON data as i need now i only want to have date not month and year.
Any one out here pleae guide me or suggest me how can i achieve this
Share Improve this question asked Jan 2, 2019 at 5:39 manish thakurmanish thakur 82010 gold badges43 silver badges83 bronze badges 5-
1
have you tried
new Date(date).getDay();
– Bibberty Commented Jan 2, 2019 at 5:45 - Please try to ask question with minimal code. – A.T. Commented Jan 2, 2019 at 5:47
-
Even
date.slice(-2)
will get the data you want – Phil Commented Jan 2, 2019 at 5:48 - Possible duplicate of Extract date and time from string using Javascript – A.T. Commented Jan 2, 2019 at 5:48
- Possible duplicate of Get exact day from date string in Javascript – Dinesh K Commented Jan 2, 2019 at 9:32
8 Answers
Reset to default 3var date = new Date('2019-01-02');
alert(date.getDate())
.getDate()
is the function which returns the day from a date.
If the date is string do-
console.log(date.slice(-2));
You can first convert tour date string to Date Object and then use getDate()
to get the date. This would guarantee support for various date-time format.
var date = new Date('2018-09-01')
console.log(date.getDate());
As per your array raw
data, assuming your date will always be at the 1 index, you can try this
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)
You can change the date string to Date object and use the getDate function to get the date part
var d = new Date('2018-09-03');
console.log(d.getDate())
Try adding this:
new Date("2018-09-03").getDate();
I will tell you to get your raw array in desired form in the first statement itself as follows.
Note: Then you move forward with you logic after that. Creating new dates using
new Date()
statement doesn't make sense.
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' ] ]
*/
Try This code
var weekday = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var d = new Date('08/11/2015');
alert(weekday[d.getDay()]);
You can use moment.js which is simple to use:
<script src="https://momentjs./downloads/moment.js"/>
var day = moment(new Date('2018-09-03')).format('DD');
You can acplish this through a little regex magic:
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])
}
The regex matches your day string (4 digits "-" 2 digits "-" 2 digits) and captures the date before pushing it to a array.
https://developer.mozilla/en-US/docs/Web/JavaScript/Guide/Regular_Expressions