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

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 '';
    }

 

This helped me, so thank you!

There is even a better solution for the formatted time return I found while poking around.

var time = myDate.getUserFormattedLocalTime();

This ensures that even if the system isn't formatted on way or another it will get all the correct for local time.