set a specific time for a date

El Cuchi
Tera Guru

hi all

i do have a business rule that inserts a scheduled event the day an outage starts and at the start of the outage.

no issues to insert the event at the time the outage starts, but i cannot get the one at 00:00:00 of the day of the outage.

 

here is my code.

	var outbegin = current.begin;
        var samed = new GlideRecord(outbegin);
	var theTime = '00:00:00';
        var theDate = new GlideDate(outbegin) + ' 00:00:00';
	gs.log('same day is ' + theDate);


       if (samed== new GlideDate()){
		gs.eventQueueScheduled("change.outage",current,'0',' ',theDate);
		gs.eventQueueScheduled("change.outage",current,'now',' ',samed);
	}
1 ACCEPTED SOLUTION

Arun_Manoj
Mega Sage

Hi @El Cuchi ,

 

 

  1. Incorrect use of GlideRecord and GlideDate:
    new GlideRecord(outbegin) and new GlideDate(outbegin) are not valid. GlideRecord is used to query a table, and GlideDate cannot be instantiated directly with a string. To work with date and time values, you should use GlideDateTime.

  2. String concatenation for dates:
    Adding a time string to a GlideDate object ('00:00:00') won't work as intended. You need to construct a proper GlideDateTime object.

Logic error in if (samed == new GlideDate()):
This condition doesn't make sense in the context. samed is not properly defined or compared.

 

var outbegin = current.begin; // Assume 'begin' is a valid GlideDateTime field
var outBeginDateTime = new GlideDateTime(outbegin);

// Extract the date part from the start time and set time to '00:00:00'
var startOfDay = new GlideDateTime(outbegin);
startOfDay.setDisplayValue(startOfDay.getDate() + " 00:00:00");

// Log the start of the day for debugging
gs.log('Start of the day: ' + startOfDay);

// Queue the scheduled events
gs.eventQueueScheduled("change.outage", current, startOfDay, " ");
gs.eventQueueScheduled("change.outage", current, outBeginDateTime, " ");

 

  1. GlideDateTime usage:
    GlideDateTime is used to handle both date and time values.

  2. Setting time to 00:00:00:
    The setDisplayValue method helps set the time to midnight (00:00:00) for the start of the day.

  3. Queueing the events:
    Events are queued using the corrected startOfDay for midnight and outBeginDateTime for the actual start time of the outage.

The solution is helpful , Please mark it as Helpful.

 

 

Thanks & Regards

Arun Manoj

View solution in original post

2 REPLIES 2

dgarad
Giga Sage

Hi @El Cuchi 

Refer the below link.

https://www.servicenow.com/community/developer-forum/set-datetime-value-with-00-00-00-time/m-p/23659...

If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

Arun_Manoj
Mega Sage

Hi @El Cuchi ,

 

 

  1. Incorrect use of GlideRecord and GlideDate:
    new GlideRecord(outbegin) and new GlideDate(outbegin) are not valid. GlideRecord is used to query a table, and GlideDate cannot be instantiated directly with a string. To work with date and time values, you should use GlideDateTime.

  2. String concatenation for dates:
    Adding a time string to a GlideDate object ('00:00:00') won't work as intended. You need to construct a proper GlideDateTime object.

Logic error in if (samed == new GlideDate()):
This condition doesn't make sense in the context. samed is not properly defined or compared.

 

var outbegin = current.begin; // Assume 'begin' is a valid GlideDateTime field
var outBeginDateTime = new GlideDateTime(outbegin);

// Extract the date part from the start time and set time to '00:00:00'
var startOfDay = new GlideDateTime(outbegin);
startOfDay.setDisplayValue(startOfDay.getDate() + " 00:00:00");

// Log the start of the day for debugging
gs.log('Start of the day: ' + startOfDay);

// Queue the scheduled events
gs.eventQueueScheduled("change.outage", current, startOfDay, " ");
gs.eventQueueScheduled("change.outage", current, outBeginDateTime, " ");

 

  1. GlideDateTime usage:
    GlideDateTime is used to handle both date and time values.

  2. Setting time to 00:00:00:
    The setDisplayValue method helps set the time to midnight (00:00:00) for the start of the day.

  3. Queueing the events:
    Events are queued using the corrected startOfDay for midnight and outBeginDateTime for the actual start time of the outage.

The solution is helpful , Please mark it as Helpful.

 

 

Thanks & Regards

Arun Manoj