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

7 REPLIES 7

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

sumanta pal
Kilo Guru

Yes, you can use the now() function to capture the current date. However, it will capture the date in GMT timezone. If you want to capture the date in a specific timezone, you need to use the gs.nowDateTime() function and then format it according to your needs. Here is a sample code:

javascript
var gdt = new GlideDateTime();
gdt.setValue(gs.nowDateTime());
gdt.setTZ(gs.getTimeZone());
var date = gdt.getDisplayValue();
gs.info(date);


This code will capture the current date and time in the user's timezone. If you want to format it in '28-Nov-2016' format, you can use the getLocalDate() function:

javascript
var gdt = new GlideDateTime();
gdt.setValue(gs.nowDateTime());
gdt.setTZ(gs.getTimeZone());
var date = gdt.getLocalDate();
gs.info(date);


This will return the date in 'yyyy-MM-dd' format. If you want to change the format, you can use the SimpleDateFormat class:

javascript
var gdt = new GlideDateTime();
gdt.setValue(gs.nowDateTime());
gdt.setTZ(gs.getTimeZone());
var sdf = new Packages.java.text.SimpleDateFormat("dd-MMM-yyyy");
var date = sdf.format(gdt.getDisplayValue());
gs.info(date);


This will return the date in '28-Nov-2016' format.

To summarize:

- Use the now() function to capture the current date in GMT timezone.
- Use the gs.nowDateTime() function to capture the current date and time in the user's timezone.
- Use the getLocalDate() function to get the date in 'yyyy-MM-dd' format.
- Use the SimpleDateFormat class to format the date in a specific format.


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