react-bootstrap-datetimepicker icon indicating copy to clipboard operation
react-bootstrap-datetimepicker copied to clipboard

Can we use ISOString as internal format?

Open AoDev opened this issue 9 years ago • 2 comments

Hello, in our app we use ISO String for our dates. http://momentjs.com/docs/#/displaying/as-iso-string/

But I have not found a way to specify this format with dateTimePicker. As I understand, it expects a format as a string like 'DD/MM/YY' but ISOString in momentJS is a method.

I can transform to a temporary format, but I'd rather not have to do this.

so the question is how to do this?

<DateTimeField 
  inputFormat="YYYY/MM/DD h:mm A"
  format="ISOString"
  dateTime={dateAsISO} />

There are some ISOString "format string" out there but I'd rather not to have to use that neither:

http://stackoverflow.com/questions/114983/given-a-datetime-object-how-do-i-get-a-iso-8601-date-in-string-format

AoDev avatar Aug 14 '15 09:08 AoDev

You want to look at the Parsing section of moment.js : http://momentjs.com/docs/#/parsing/

Hence you need in the format prop to specify something like "YYYY/MM/DD h:mm A". inputFormat on the other just specify how the date is going to look like in the input field, I realized now that it's a bit confusing because it's like the output..

chollier avatar Aug 14 '15 16:08 chollier

Per moment.js docs, it's not necessary to specify a format to parse a date when working with ISO date strings.

And their docs also say that it's possible to specify ISO format explicitly if wanted:

" Moment already supports parsing iso-8601 strings, but this can be specified explicitly in the format/list of formats when constructing a moment."

moment("2010-01-01T05:06:07", moment.ISO_8601);

They do warn us that a format is necessary when NOT using ISO dates.

"For consistent results parsing anything other than ISO 8601 strings, you should use String + Format."

"Note: Automatic cross browser ISO-8601 support was added in version 1.5.0. Support for the week and ordinal formats was added in version 2.3.0.

So there is no need to provide a format when the input is a date with ISO-8601.

From here: http://momentjs.com/docs/#/parsing/special-formats/

I would expect the same from the react-picker component and should be able to do it this way:

let myISODate = 'Some valid ISO Date';

<DateTimeField 
  inputFormat="YYYY/MM/DD h:mm A"
  dateTime={myISODate} />

or maybe something like this:

let myISODate = 'Some valid ISO Date';

<DateTimeField 
  inputFormat="YYYY/MM/DD h:mm A"
  format={moment.ISO_8601}
  dateTime={myISODate} />

Then the onChange event should return a ISO String too.

Default format

As of version 1.5.0, calling moment#format without a format will default to moment.defaultFormat. Out of the box, moment.defaultFormat is the ISO8601 format YYYY-MM-DDTHH:mm:ssZ.

from here: http://momentjs.com/docs/#/displaying/format/

But if people want to use the UTC time, then it should be possible to ask moment to call toISOString() instead of format(). (which is my case, in our db / apps, all dates go as ISO-8601 UTC)

But currently, DatePicker shows "invalid date".

If I find some time, I can send a PR related to this issue?

AoDev avatar Aug 14 '15 21:08 AoDev