Show current date/time in YYYY-MM-DD HH:MM:SS format for a date/time field using Client Script

chatsaurav19
Tera Contributor

Hi All,

 

I am trying to populate a Date/Time field based on a condition ( using onChange Client Script ) but is not working as expected.

 

var d = new Date();
if (newValue == 'true') {
g_form.setValue('accept_time_mdm', d);
}

 

Can someone please help me how to get the field populated with the format which I have shared?

 

Regards,

Saurabh Chatterjee

1 ACCEPTED SOLUTION

Amit Gujarathi
Giga Sage
Giga Sage

HI @chatsaurav19 ,
I trust you are doing great.
Here's an updated version of your script:

var d = new Date();

// Check your condition
if (newValue == 'true') {
    // Format the date into YYYY-MM-DD HH:MM:SS
    var formattedDate = d.getFullYear() + '-' +
        ('0' + (d.getMonth() + 1)).slice(-2) + '-' + // Months are zero-based
        ('0' + d.getDate()).slice(-2) + ' ' +
        ('0' + d.getHours()).slice(-2) + ':' +
        ('0' + d.getMinutes()).slice(-2) + ':' +
        ('0' + d.getSeconds()).slice(-2);

    // Set the formatted date to the field
    g_form.setValue('accept_time_mdm', formattedDate);
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



View solution in original post

2 REPLIES 2

Amit Gujarathi
Giga Sage
Giga Sage

HI @chatsaurav19 ,
I trust you are doing great.
Here's an updated version of your script:

var d = new Date();

// Check your condition
if (newValue == 'true') {
    // Format the date into YYYY-MM-DD HH:MM:SS
    var formattedDate = d.getFullYear() + '-' +
        ('0' + (d.getMonth() + 1)).slice(-2) + '-' + // Months are zero-based
        ('0' + d.getDate()).slice(-2) + ' ' +
        ('0' + d.getHours()).slice(-2) + ':' +
        ('0' + d.getMinutes()).slice(-2) + ':' +
        ('0' + d.getSeconds()).slice(-2);

    // Set the formatted date to the field
    g_form.setValue('accept_time_mdm', formattedDate);
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Hi @Amit Gujarathi Thank you! It worked.... Can you please let me know how to achieve the same by checking the 'time zone' of the user and then populating the field based on the user's time zone?

 

Regards,

Saurabh