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

Steven Parker
Giga Sage

This post on the community should be able to assist with this:

https://www.servicenow.com/community/developer-forum/is-there-a-glidedatetime-variant-for-getbyforma... 

 

Since getByFormat function doesn't work for GlideDateTime, you have to do some other work to achieve what you are looking for.


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

Hi Steven,

 

Thanks for response.

I have tried but not getting successful.

Can you help me with code?

 

Can you paste the code you have now?


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

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