Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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

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

Brad Tilton
ServiceNow Employee

You can use GlideDateTime() for that.



var gdt = new GlideDateTime();


gs.log(gdt.getMonth());



GlideDateTime - ServiceNow Wiki


Thanks!


By the way... I found this link extremely helpful, so hopefully someone else can benefit from it too.



http://www.snc-blog.com/2011/02/07/working-with-date-date-fields-in-imports-and-business-rules/


johnram
ServiceNow Employee

The ServiceNow Wiki content is no longer supported. Updated information about this topic is located here: GlideDateTime




Visit http://docs.servicenow.com for the latest product documentation