Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Datetime-local - default value

Questions related to using nuBuilder Forte.
Post Reply
yvesf
Posts: 315
Joined: Sun Mar 14, 2021 8:48 am
Location: Geneva
Has thanked: 87 times
Been thanked: 11 times

Datetime-local - default value

Unread post 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,
kev1n
nuBuilder Team
Posts: 4303
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Datetime-local - default value

Unread post by kev1n »

Try this:

Code: Select all

nuSetValue(this.id, new Date().toJSON().slice(0,19));
yvesf
Posts: 315
Joined: Sun Mar 14, 2021 8:48 am
Location: Geneva
Has thanked: 87 times
Been thanked: 11 times

Re: Datetime-local - default value

Unread post by yvesf »

Thanks kev1n !!! it's the solution. Could you explain me why datetime-local behaves differently than date ?
kev1n
nuBuilder Team
Posts: 4303
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Datetime-local - default value

Unread post by kev1n »

datetime-local expects a format like this: '2022-04-15T05:26:50'
yvesf
Posts: 315
Joined: Sun Mar 14, 2021 8:48 am
Location: Geneva
Has thanked: 87 times
Been thanked: 11 times

Re: Datetime-local - default value

Unread post 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));
}
kev1n
nuBuilder Team
Posts: 4303
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Datetime-local - default value

Unread post 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.
Post Reply