We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

How to get current date..now()?

aarav
Kilo Contributor

Hi friends.

I want to capture the current date to one of my field.

We can capture the time from nowDateTime() function.

But i want to capture directly   In 28-Nov-2016 format.

can we use           function   : now()

But it says 'Gets the current date using GMT date time.'   .

My question is will it capture current date?     as its mentioned GMT...will it capture different date.....

if yes then in what format it will capture..

I am not able to produce/create scenario here to test...

Please advice

Thank you very much in advance.

regards

Aarav

6 REPLIES 6

Ramesh Lohar
Kilo Guru

Yes, you can use the now() function to capture the current date. However, it will capture the date in GMT format. If you want to capture the date in a specific format like '28-Nov-2016', you will need to format the date after capturing it. Here's how you can do it:

1. Use the now() function to capture the current date.
2. The now() function will return the date in GMT format. It will not capture a different date.
3. The format in which the date is captured will be 'yyyy-MM-dd HH:mm:ss' (e.g., '2016-11-28 00:00:00').
4. To format the date in '28-Nov-2016' format, you can use the following code:

javascript
var gdt = new GlideDateTime(); // this will get the current date and time
gdt.setDisplayValue(gdt.getDisplayValue()); // this will set the value to the current date and time
var date = gdt.getDisplayValue().split(' ')[0]; // this will get the date part of the date and time
var formattedDate = date.split('-')[2] + '-' + date.split('-')[1] + '-' + date.split('-')[0]; // this will format the date in '28-Nov-2016' format


5. Now, you can use the formattedDate variable to set the value of your field.

Please note that the above code will work if the date format of your instance is 'yyyy-MM-dd'. If the date format is different, you will need to adjust the code accordingly.


nowKB.com

Rajdeep Ganguly
Mega Guru

Yes, you can use the now() function to capture the current date. However, it will capture the date in GMT format. If you want to capture the date in a specific format like '28-Nov-2016', you will need to format the date. Here is how you can do it:

1. Use the now() function to capture the current date.
2. The now() function will return the date in GMT format.
3. To format the date, you can use the getDisplayValue() function.
4. The getDisplayValue() function will return the date in the user's time zone and in the format specified in the user's profile.

Here is a sample code:

javascript
var gdt = new GlideDateTime(); // This will get the current date and time
gdt.setDisplayValue(gdt.getDisplayValue()); // This will format the date in the user's time zone and format
var date = gdt.getDisplayValue().split(' ')[0]; // This will get the date part


Please note that the date format will depend on the user's profile settings. If you want a specific format like '28-Nov-2016', you may need to format the date manually.


nowKB.com