Change date format in datetime field

Nowlearner
Kilo Guru

Hi,

I am trying to change only date format in my datetime field in a notification. For this i am using an email script.

Here is the current format : 2-1-2022 11:21:56 AM EDT, i want it to be changed to 2/1/2022 11:21:56 AM EDT

I dont want to make any changes to time format, but i couldnt achieve it, below is the code i tried.

(function runMailScript(current, template, email, email_action,event) {

    var openedAt = current.opened_at;
	template.print(convertTime(openedAt));

    function convertTime(inTime) {
        if (inTime != '') {
            var myDate = new GlideDateTime(Date);
            var time = myDate.getTime();
		return myDate.getDate().getByFormat("M/dd/yyyy") + ' ' + time;
        }
        return '';
    }

})(current, template, email, email_action, event);
1 ACCEPTED SOLUTION

Community Alums
Not applicable

Do you have your system set up so that it displays EDT?  If so, you should be able to add .getDisplayValue() after .getTime to display it.  

 

function convertTime(inTime) {
        if (inTime != '') {
            var myDate = new GlideDateTime(inTime);
            var time = myDate.getTime().getDisplayValue();
		return myDate.getDate().getByFormat("M/dd/yyyy") + ' ' + time;
        }
        return '';
    }

 

View solution in original post

6 REPLIES 6

Community Alums
Not applicable

Remove the word Date in this line

var myDate = new GlideDateTime(Date);

 

var myDate = new GlideDateTime();

I receive this when i changed it.

Actual format: 2018-06-20 10:26:34 AM EDT

Changed format: 2/01/2022 1970-01-01 22:34:20

This is not what i was looking for. Is it possible to change only the data format to mm/dd/yyyy?

Community Alums
Not applicable

Sorry, try this:

var myDate = new GlideDateTime(inTime);

I get the same.

Actual format: 2017-07-25 04:37:48 PM EDT

Changed format: 7/25/2017 1970-01-01 20:37:48

in process of changing the date format, the time is displaying as wrong. It should be the same as in "actual format" time.