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

-O-
Kilo Patron

Could you explain the business need?

System date on client side sounds confusing and the need to format a date specifically also is not something normally done.

Ajay Singh2
Tera Contributor

On selection of Select User the top StackName field is populating in that field i need datetime like this.

AjaySingh2_0-1699042987277.png

 

 

I guess the safest option is to do what @Samaksh Wani suggested: get the date from server side, using GlideAjax.

While you could get the current date purely on client side, e.g:

alert(getNowDateTime());

function getNowDateTime () {
	var dateOut = new Date();
	return formatDate(dateOut, 'yyyyMMdd-HHmmss');
}

it can be that the current user's current device doesn't have the correct date/time or time zone and could lead to wrong results.

Ajay Singh2
Tera Contributor

can you provide code to use in script include and client script for this. Currently i am getting date with this script. See below screenshot

AjaySingh2_0-1699044062906.png

 

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