2

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

manish thakur
  • 760
  • 9
  • 35
  • 76
  • 1
    have you tried `new Date(date).getDay();` – Bibberty Jan 02 '19 at 05:45
  • Please try to ask question with minimal code. – A.T. Jan 02 '19 at 05:47
  • Even `date.slice(-2)` will get the data you want – Phil Jan 02 '19 at 05:48
  • Possible duplicate of [Extract date and time from string using Javascript](https://stackoverflow.com/questions/14787271/extract-date-and-time-from-string-using-javascript) – A.T. Jan 02 '19 at 05:48
  • Possible duplicate of [Get exact day from date string in Javascript](https://stackoverflow.com/questions/17038105/get-exact-day-from-date-string-in-javascript) – Dinesh K Jan 02 '19 at 09:32

8 Answers8

3
var 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));
ellipsis
  • 12,049
  • 2
  • 17
  • 33
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)
Avi
  • 2,014
  • 1
  • 9
  • 21
  • hey can you please help me out https://stackoverflow.com/questions/56521258/how-to-display-image-dynamically-on-ui I have been stuck here from long time, If you can help me with some approach than it will be very helpful – manish thakur Jun 10 '19 at 08:02
1

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())
Vineesh
  • 3,762
  • 20
  • 37
  • hey can you please help me out on this post https://stackoverflow.com/questions/55919420/how-to-make-html-table-expand-on-click – manish thakur May 06 '19 at 05:36
0

Try adding this:

new Date("2018-09-03").getDate();
Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
Dinesh K
  • 641
  • 1
  • 8
  • 20
0

You can accomplish 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.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

James_F
  • 449
  • 2
  • 8
0

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' ] ]
*/
hygull
  • 8,464
  • 2
  • 43
  • 52
0

Try This code

var weekday = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var d = new Date('08/11/2015');
alert(weekday[d.getDay()]);
Rinto Antony
  • 297
  • 1
  • 10
0

You can use moment.js which is simple to use:

<script src="https://momentjs.com/downloads/moment.js"/>

var day = moment(new Date('2018-09-03')).format('DD');
Umesh
  • 2,704
  • 19
  • 21