How to change date format from DD-MM-YYYY to DD/MM/YYYY

lucky24
Tera Contributor

Hi Team,

I have 2 date field start date and end date,

end date populate automatically according to start date and adding 15 days in end date accordingly start date.

I have written one script but I am unable to convert it into DD/MM/YYYY format.

 

var start_date = this.getParameter('sysparm_start_date');
var two_weeks_from_now = new GlideDateTime(start_date);
two_weeks_from_now.addDays(15);
return two_weeks_from_now;

 

From above script I am getting date in DD-MM-YYYY but I want date in DD/MM/YYYY format.

Can Somebody help me here please.?

1 ACCEPTED SOLUTION

Try this:

var start_date = this.getParameter('sysparm_start_date');
var two_weeks_from_now = new GlideDateTime(start_date);
two_weeks_from_now.addDays(15);
//Now lets format it
var format = 'dd/MM/yyyy hh:mm:ss'.split(' ');
var gdt = new GlideDateTime(two_weeks_from_now);
var formatDate = format[0];
var formatTime = format[1];
var gd = gdt.getLocalDate().getByFormat(formatDate);
var gt = gdt.getLocalTime().getByFormat(formatTime);
var output = gd + ' ' + gt;
return output;

Please mark this response as correct and/or helpful if it assisted you with your question.
Steven

View solution in original post

6 REPLIES 6

Thank you so much Steven, This is working for me.

Prince Arora
Tera Sage

@lucky24 

 

can you try below code:

 

var date = ("yourDate").toString(); //lets say "01-12-2023"

date = (date.split("-")).join("/");

 

Please Accept the solution + mark it as helpful if it has answered your query.