0

I have a form that sends the value of year and months from an input and then while sending the value to the server I am converting that values to ISO string like that:

const toIsoString = (year, month, day) => moment(new Date(year, month - 1, day)).toISOString(true).split('.')[0];

And then in the values I am using it like this.

StartDate: toIsoString(data.StartYear, parseInt(data.StartMonth, 10), 1),

In that case It is sending the value like this:

startDate: "2021-01-01T00:00:00"

Does anybody know why the Time period is being ignored and how can I also send the time period with the year, month and date values.Any helps would be highly appreciated.Thanks...

ninja
  • 167
  • 2
  • 12

2 Answers2

2

Does anybody know why the Time period is being ignored and how can I also send the time period with the year, month and date values.Any helps would be highly appreciated.

The time isn't ignored. In the function:

const toIsoString = (year, month, day) => 
  moment(new Date(year, month - 1, day)).toISOString(true).split('.')[0];

the values for hour, minute, second and millisecond are omitted so they default to 0. What time are you expecting?

If you want the current local time added to the date, then create a date and set the year, month and day to the required values without modifying the time (though I don't know why you'd want to do that).

Rather than creating a string that you then need to further process, tell moment.js the format you want:

function toIsoString (year, month, day) { 
  return moment(new Date().setFullYear(year, month-1, day)).format('YYYY-MM-DD HH:mm:ss');
}

console.log(toIsoString('2021','1','1'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.26.0/moment.min.js"></script>

You can also do that without a library, see How to format a JavaScript date, e.g.:

function formatDate(year, month, date) {
  let z = n => (n<10?'0':'') + Number(n);
  return `${year}-${z(month)}-${z(date)} ${
    new Date().toLocaleString('en',{
      hour12:false,
      hour:'2-digit', 
      minute:'2-digit', 
      second:'2-digit'})
  }`;
}

console.log(formatDate('2021','1','01'))
RobG
  • 142,382
  • 31
  • 172
  • 209
0

Its because you are only setting year, month and date while creating moment object. You are not setting time

You should do something like

const toIsoString = (year, month, day) => {
    const currDate = moment(new Date());
    currDate.year(year);
    currDate.month(month - 1);
    currDate.date(day);
    return currDate.toISOString(true).split('.')[0];
}

Or simply use set function

const toIsoString = (year, month, day) => {
    const currDate = moment(new Date());
    currDate.set({
        'year': year,
        'month': (month - 1),
        'date': day
    });
    return currDate.toISOString(true).split('.')[0];
}
Kiran Shinde
  • 5,732
  • 4
  • 24
  • 41
  • does that include the hour,minute,seconds, at least I have tried that and still getting the same response – ninja May 30 '20 at 10:13
  • what did you tried? Can you update you your code? As far I know these two techniques should work – Kiran Shinde May 30 '20 at 10:22
  • I have replaced my toIsoString with your's but when sending the value it seems that time period is still ignored,, – ninja May 30 '20 at 16:19
  • shall I also make some changes here StartDate: toIsoString(data.StartYear, parseInt(data.StartMonth, 10), 1), – ninja May 30 '20 at 16:36