ITSM

ayushigupta9
Tera Contributor

I have one date time field which is in format "01/27/2024 15:40:20" and i want it to show like "2024/01/27 15:40:20"

How to achieve this

 

Screenshot 2024-01-27 at 3.41.35 PM.png

Thanks in advance.

7 REPLIES 7

chetanb
Tera Guru

Hello @ayushigupta9 ,

 

Did you check with GlideDateTime object.

var dateTimeValue = current.your_date_time_field;

 

var gdt = new GlideDateTime();

gdt.setDisplayValue(dateTimeValue);

var formatted = gdt.getDisplayValue('yyyy/MM/dd HH:mm:ss');

 

current.formatted_date_time_field = formatted;

 

If my answer solve your query kindly accept and mark as correct and helpful.

 

Regards,

Cb

I have used this code as Before BR but it is showing below error

Screenshot 2024-01-27 at 4.14.28 PM.png

@ayushigupta9 

Please use the below Code which is working fine in the Background Script .

var gdt = new GlideDate();
gs.print(gdt);

 

Si11_0-1706356532489.png

 

Regards,

Rudransh.


var date = gdt.getByFormat('dd/MM/yyyy');
gs.print(date);

 

You can use

 

var d = new Date();

// 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);

 

or you may check -

https://www.servicenow.com/community/developer-forum/how-to-convert-the-date-time-format-to-mm-dd-yy...

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.

 

Regards,

CB