Home > Mobile >  How to get raw date string from date picker?
How to get raw date string from date picker?

Time:01-12

I'm struggling for hours with this seemingly trivial issue.

I have a antd datepicker on my page.

Whenever I choose a date, instead of giving me the date I chose, it gives me a messy moment object, which I can't figure out how to read.

All I want is that when I choose "2020-01-18", it should give me precisely this string that the user chose, regardless of timezone, preferably in ISO format.

This is not a multi-national website. I just need a plain vanilla date so I can send it to the server, store in db, whatever.

Here are some of my trials, so far no luck:

var fltval = e;
if (isMoment(fltval)) {
  var dat = fltval.toDate();
  //dat.setUTCHours(0)
  fltval = dat.toISOString(); // fltval.toISOString(false)

  var a = dat.toUTCString();
  //var b = dat.toLocaleString()
}

It keeps on moving with a few hours, probably to compensate for some timezone bias

UPDATE 1:

the datestring is data-wise correct. But its not ISO, so I cant use it correctly. I might try to parse this, but I cannot find a way to parse a string to date with a specific format.

Thanks!

CodePudding user response:

There are 2 ways to get the date string:

  1. Use the moment.format api: date.format("yyyy-MM-DD")
  2. Use the date string that is passed to the onChange as second parameter

Here is a Link.

CodePudding user response:

I am assuming your code snippet is inside the onChange method. This gives you a moment and a date string to work with (the first and second parameters of the function respectively).

You have a few options. You could set the format prop on the DatePicker to match the format of the string you want. Then just use the date string. Or you can use the moment object as Domino987 described.

CodePudding user response:

Javascript date functions can be used,

I assume you are getting in 2022-01-03T11:19:07.946Z format then

date.toISOString().slice(0, 10)

to 2022-01-03

  •  Tags:  
  • Related