Change Date Format

Drishti
Tera Guru

Hello, 

I am getting date in string as 01-Feb-99. How can I convert this date into dd-mm-yyyy?

Please guide.

 

Thanks in advance !

10 REPLIES 10

Fabian Kunzke
Mega Sage

Hey Drishti,

 

You can use the GlideDate.getByFormat() function for this. A more extensive use-case example can be found here.

 

Regards

Fabian

its_SumitNow
Kilo Sage

Hi @Drishti 

 

// Your input date string
var dateStr = "01-Feb-99";

// Create a GlideDateTime object
var gdt = new GlideDateTime();
gdt.setDisplayValue(dateStr);

// Get the date in yyyy-MM-dd format first
var date = gdt.getDate();

// Parse and reformat to dd-mm-yyyy
var parts = date.toString().split("-");
var formattedDate = parts[2] + "-" + parts[1] + "-" + parts[0];

gs.info("Formatted Date: " + formattedDate); // Output: 01-02-1999

 if for server side than you can use this, this code snippet works

hope this helps, if helped please mark helpful and Accept as solution 🙂

 

Warm Regards

Sumit

Technical Consultant

Hey,

Using GlideDate.getByFormat() as from this example is much better in this case - especially when using the GlideDate object already.

Regards

Fabian

Ankur Bawiskar
Tera Patron

@Drishti 

try this in background Script

var dateString = '01-Feb-99';
var gd = new GlideDate();

// Step 1: Parse input string using display format
gd.setDisplayValue(dateString, 'dd-MMM-yy');  // Input format pattern

// Step 2: Output in desired dd-mm-yyyy format  
var formattedDate = gd.getByFormat('dd-MM-yyyy');
gs.info('Formatted: ' + formattedDate);  // Output: 01-02-1999

AnkurBawiskar_0-1768553692371.png

 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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