The CreatorCon Call for Content is officially open! Get started here.

How to Change Date Format 9/27/2023 to MM-dd-yyyy?

Community Alums
Not applicable

I have used below code to convert the date format but didn't worked. 

var gr = '09/27/2023';

var gd = new GlideDate(); 
gd.setValue(gr);
gs.info(gd.getByFormat("MM-dd-yyyy"));

But Getting below error in background script:

"Unparseable date: "09/27/2023": java.text.ParseException:"

Please help me on this!

1 ACCEPTED SOLUTION

Jaspal Singh
Mega Patron
Mega Patron

Hi Ganesh,

 

Can you try below in bacgkground script once for a check.

var gr = '09/27/2023';
var gd = new GlideDateTime(gr).getDate(); 
//gd.setValue(gr);
gs.info(gd.getByFormat("MM-dd-yyyy"));

 

View solution in original post

4 REPLIES 4

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Where is this 09/27/2023 coming from? Can you provide more details? This is not what's actually stored in the XML of the record, correct?

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Jaspal Singh
Mega Patron
Mega Patron

Hi Ganesh,

 

Can you try below in bacgkground script once for a check.

var gr = '09/27/2023';
var gd = new GlideDateTime(gr).getDate(); 
//gd.setValue(gr);
gs.info(gd.getByFormat("MM-dd-yyyy"));

 

Community Alums
Not applicable

@Jaspal Singh Thank You so much for your reply. 

Ankur Bawiskar
Tera Patron
Tera Patron

@Ganesh Manasali 

updated script below

var gr = '09/27/2023';
var gdt = new GlideDateTime();
gdt.setDisplayValue(gr, "MM/dd/yyyy");
var formattedDate = gdt.getDate();
var time = new GlideDate();
time.setValue(formattedDate);
gs.info(time.getByFormat("MM-dd-YYYY"));

Output:

find_real_file.png

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader