-2

Please find attachment. The actual date is 03/06/2019 but the returning formate is always 01/01/1970. How can I resolve this issue?

enter image description here

apiData.map(res => {
const startDate = moment(res.Issue_Date).format('DD/MMM/YYYY');
const endDate = moment(res. Expire_Date).format('DD/MMM/YYYY');});

Note: I am getting the date from API call like this (Issue_Date: 20190603)

Vega
  • 27,856
  • 27
  • 95
  • 103
imjayabal
  • 805
  • 1
  • 12
  • 24

3 Answers3

1

You need to convert the Issue_Date to the String type. You are passing in the number which is considered as Unix timestamp and therefore that's why you are getting the Jan 01 1970, 11:06:30 (20190603 milliseconds passed from Jan 01 1970, 00:00:00).

moment(res.Issue_Date.toString()); // or moment(`${res.Issue_Date}`);
grizlizli
  • 137
  • 7
0

try the following

apiData.map(res => {
const startDate = moment(res.Issue_Date,'YYYY-MM-DD').format('DD/MMM/YYYY');
const endDate = moment(res. Expire_Date,'YYYY-MM-DD').format('DD/MMM/YYYY');});
0

Ok, the problem is your issue date is getting passed as number format, so moment is calculating in milliseconds. Try this -

let temp = res.Issue_Date.toString();
const startDate = moment(temp ).format('DD/MMM/YYYY');
console.log(startDate)
Ram
  • 356
  • 2
  • 14