gs.nowDateTime() getting year/month/day separately

xiaix
Tera Guru

I have a custom table with a Date/Time field named PressedButton.

This table also has an Integer field named Year, an String field named Month, and an Integer field named Day.

When I run this:

var nowdt = gs.nowDateTime();

var gr = new GlideRecord('u_my_custom_table');

gr.u_pressedbutton = nowdt;

gr.insert();

Everything works great.   The record is inserted and the date/time field shows up perfectly.

I now want to populate the other fields with the breakdown of gs.nowDateTime().   For example:

var nowdt = gs.nowDateTime();

var gr = new GlideRecord('u_my_custom_table');

gr.u_pressedbutton = nowdt;

gr.u_year = gs.nowDateTime(yyyy);

gr.u_month = gs.nowDateTime(m);

gr.u_day = gs.nowDateTime(dd);

gr.insert();

But as we all know, my parameters in gs.nowDateTime() are invalid.   I've searched multiple community posts and the SN wiki, but can't find a simple and clean method (as exampled above) on how to do this.

Any suggestions?

1 ACCEPTED SOLUTION

Brad Tilton
ServiceNow Employee
ServiceNow Employee

You can use GlideDateTime() for that.



var gdt = new GlideDateTime();


gs.log(gdt.getMonth());



GlideDateTime - ServiceNow Wiki


View solution in original post

7 REPLIES 7

The link to docs.servicenow.com is laughable.. no offense.   Wiki is far more helpful.   docs.sn needs a major overhaul with usability and is severely deprived of meaningful content.   I'm also a little surprised you would necro a 2-year old answered question to post this silliness.


johnram
ServiceNow Employee
ServiceNow Employee

Just doing what I'm told to do


Hey, don't take it personally   I'm just being feisty today!   ::pokes::