The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Get today's date in MM-DD-YYYY format without time.

hars
Kilo Expert

How to get today's date in MM-DD-YYYY format without time in script?

1 ACCEPTED SOLUTION

hars
Kilo Expert

var currentdate = new GlideDate();
currentdate.getByFormat("MM-dd-yyyy");

View solution in original post

9 REPLIES 9

result came back as: 2020-02-19  or yyyy-mm-dd

sachin_namjoshi
Kilo Patron
Kilo Patron

IF you want in business rule, then use below code to get current time without time 

Set date via business rule action

IF you want in client script, then use below code to get current date without time

 

https://community.servicenow.com/community?id=community_question&sys_id=38c40be9dbd8dbc01dcaf3231f9619e3

 

Regards,

Sachin

hars
Kilo Expert

var currentdate = new GlideDate();
currentdate.getByFormat("MM-dd-yyyy");

Hi Hars,

I have used your code, its working but, I want to change my date format to "dd-MM-yyyy". when I changed your code as below its now working.

var currentdate = new GlideDate();

currentdate.getByFormat(''dd-MM-yyyy");

 

I am getting 09-10-2021. My current system date is 19-06-2019.  Can you plz suggest.

 

Regards,

Sandeep Reddy Lebaka.

Ramesh Lohar
Kilo Guru

Sure, you can use the following JavaScript code in ServiceNow to get today's date in MM-DD-YYYY format without time:

javascript
var gdt = new GlideDateTime();
var date = gdt.getLocalDate();
var formattedDate = date.getMonth() + '-' + date.getDay() + '-' + date.getYear();
gs.info(formattedDate);


Here are the steps:

- Create a new GlideDateTime object.
- Use the getLocalDate() method to get the current date.
- Use the getMonth(), getDay(), and getYear() methods to get the month, day, and year respectively.
- Concatenate these values with '-' to get the date in MM-DD-YYYY format.
- Use the gs.info() method to log the formatted date.


nowKB.com