How to formate datetime from this 2023-11-03 22:51:34 to this 20231103-225134.

Ajay Singh2
Tera Contributor

Can anyone explain how to use datetime in from this 2023-11-03 22:51:34 to this 20231103-225134.formate on onchange client script.

2 ACCEPTED SOLUTIONS

Hello @Ajay Singh2 

 

It seems you have already brought the datetime

 

Just use the below code :-

 

var gdt = g_user_date_time_format;
var dateTime = gdt.split(' ');
var date=dateTime[0].split('-');
var time=dateTime[1].split(':');
var res1= date[0]+date[1]+date[2];
var res2= time[0]+time[1]+time[2];
var res=res1 +'-' +res2;
alert(res);

View solution in original post

@Ajay Singh2Nah, if you would be using my code, you would have the date formatted as desired in there.

View solution in original post

18 REPLIES 18

can i use like this 

var today_date = new Date();
    var res = formatDate(today_date, g_user_date_time_format);
    var gdt = g_user_date_time_format;
    var dateTime = gdt.split(' ');
    var date=dateTime[0].split('-');
    var time=dateTime[1].split(':');
    var res1= date[0]+date[1]+date[2];
    var res2= time[0]+time[1]+time[2];
    var res=res1 +'-' +res2;
    alert(res);

@Samaksh WaniHave you tested that code?

I guess you don't know that g_user_date_time_format is a variable set by ServiceNow in (Catalog) Client Scripts and contains exactly what the name says: the date/time format the user prefers?

E.g. for me g_user_date_time_format contains the string dd/MM/yyyy HH:mm:ss.

Ajay Singh2
Tera Contributor

yes after using your code it is populating like this yyyymmmdd but i want that in date formate. See below screenshot.

AjaySingh2_0-1699045983688.png

 

@Ajay Singh2Nah, if you would be using my code, you would have the date formatted as desired in there.