Page 1 of 1

Datetime-local - default value

Posted: Wed Apr 13, 2022 10:05 pm
by yvesf
For a nuDate, putting a date by default is done behind onnuload() by :

Code: Select all

if (nuIsNewRecord()) {
   nuSetDateValue(this.id);
}
It works perfectly.
For a Datetime-local, having change the date format as datetime in the sql db, doing the same approach doesn't work. How can you do that in Datetime-local object ?
Many thanks,

Re: Datetime-local - default value

Posted: Thu Apr 14, 2022 6:44 am
by kev1n
Try this:

Code: Select all

nuSetValue(this.id, new Date().toJSON().slice(0,19));

Re: Datetime-local - default value

Posted: Thu Apr 14, 2022 6:44 pm
by yvesf
Thanks kev1n !!! it's the solution. Could you explain me why datetime-local behaves differently than date ?

Re: Datetime-local - default value

Posted: Fri Apr 15, 2022 7:27 am
by kev1n
datetime-local expects a format like this: '2022-04-15T05:26:50'

Re: Datetime-local - default value

Posted: Tue Apr 19, 2022 10:44 am
by yvesf
To complete the answer : the problem is that new Date is in UTC and we have to localize the datetime. Here is the answer :

Code: Select all

if (nuIsNewRecord()) {
    var ld = new Date(new Date().getTime() - new Date().getTimezoneOffset() * 60000) // you keep the timezone difference with UTC and you add this to
        //the date.--- ld stands for local date ---
    nuSetValue(this.id, ld.toJSON().slice(0, 16));
}

Re: Datetime-local - default value

Posted: Fri Apr 22, 2022 1:47 pm
by kev1n

Code: Select all

new Date(new Date().getTime() - new Date().getTimezoneOffset() * 60000)
returns
Fri Apr 22 2022 15:46:02 GMT+0200 (Central European Summer Time)
But the current UTC time is 11:45 and my local time is 13:45.