GlideDateTime dd.MM.yyyy format

Jim60
Tera Expert

GlideDateTime is working great for me in my GlideAjax SI until someone selects this dd.MM.yyyy format, then it fails.

  var myDate= new GlideDateTime(gs.nowDateTime());

  myDate.setDisplayValue(<user formatted date>);

//get system format

myDate.getValue();

//get user format

myDate.getDisplayValue();

Has any one else run into this?

Is this format common to any specific regions or is it completely user preference?

If I choose to remove this format from user selection where could I do this?

I could likely try a replace on the periods to hashes and get the system date to work but not sure that I could get it to return DisplayValue back to the user without that method continuing to have an issue.

1 ACCEPTED SOLUTION

Jim60
Tera Expert

Thank you for your responses.   I found the problem in declaring a new GlideDateTime.   We had it declared as new GlideDateTime(gs.nowDateTime()).   When I removed the gs.nowDateTime parameter the problem went away.   IE. new GlideDateTime().   While the way it was worked fine with other formats this specific format failed.   When the new GlideDateTime is created it by default has the current system date/time.


View solution in original post

3 REPLIES 3

Chuck Tomasi
Tera Patron

Hi Jim,



I've been playing around in scripts background with your script (or a variant of it below.) Can you be specific about what breaks with the format? I'm seeing consistent output (save timezones) with the date.



var myDate= new GlideDateTime(gs.nowDateTime());


  myDate.setDisplayValue('08.06.2016');



//get system format


gs.print(myDate.getValue());


//get user format


gs.print(myDate.getDisplayValue());



*** Script: 2016-06-08 17:39:36


*** Script: 2016-06-08 10:39:36


The SN Nerd
Giga Sage
Giga Sage

I get the same results as Jim.



Try the workaround below.


This does assume the user isn't going to do MM.dd.yyyy


Output is shown in yyyy-MM-dd HH:mm:ss



var myDate = new GlideDateTime();


myDate.setDisplayValue(userFormat);


if (/[0-9]{2}\.[0-9]{2}\.[0-9]{4}/.test(userFormat) ) {


gs.print('dd.MM.yyyy Pattern Found');


  myDate.setDisplayValue(userFormat, "dd.MM.yyyy")


} else {


  myDate.setDisplayValue(userFormat);


}



gs.print (myDate.getDisplayValue());



userFormat = '01.02.2017'


(dd.MM.yyyy)


dd.MM.yyyy Pattern Found


2017-02-01 00:00:00


userFormat = '01-02-2017'


(MM.dd.yyyy)


2017-01-02 00:00:00



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Jim60
Tera Expert

Thank you for your responses.   I found the problem in declaring a new GlideDateTime.   We had it declared as new GlideDateTime(gs.nowDateTime()).   When I removed the gs.nowDateTime parameter the problem went away.   IE. new GlideDateTime().   While the way it was worked fine with other formats this specific format failed.   When the new GlideDateTime is created it by default has the current system date/time.