Is there any way to set the value of the Hour of a Date/Time field

Alon Grod
Tera Expert

Hi,

I have the field u_actual_date of type Date/Time.

Is there any way to set the Hour for the field? Im not talking about gdt.addHour, Im asking if i can set the hour without adding hours to the current hour using 'set' command or something similar

4 REPLIES 4

Community Alums
Not applicable

Hi @Alon Grod ,

 

Can you please try using the below code-

var u_actual_date = current.u_actual_date.getGlideObject().getLocalDate();
var dateVal = new GlideDateTime(u_actual_date);
dateVal.addDaysUTC(-1);
dateVal.setHourOfDay(17); // Set the hour to 5 PM
dateVal.setMinute(0); // Set the minute to 0
current.setDate = dateVal.getDisplayValue(); // Set the "Date" field value

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

swathisarang98
Giga Sage
Giga Sage

Hi @Alon Grod ,

 

You can try adding seconds as shown below,

swathisarang98_0-1715553457559.png

 

 

(function executeRule(current, previous /*null when async*/ ) {
    var dateTime = current.u_glide_date_time_1; //fetch the date use ur backend name
    var gdt = new GlideDateTime(dateTime);
    var hours = 60 * 60 * 5;//adding 5 hours
    gdt.addSeconds(hours);
    current.u_glide_date_time_1 = gdt;//seeting the value

})(current, previous);

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

Bert_c1
Kilo Patron

The context of want to do is missing. If you want script logic, you can try the above.  I tried:

 

var myDateTime = new GlideDateTime();
gs.info("myDateTime = " + myDateTime);
// convert to string
var myDT = myDateTime.getValue();
gs.info("myDT = " + myDT);
// set hours to 12
var newDT = myDT.substr(0,11) + "12" + myDT.substr(13);
gs.info("newDT = " + newDT);
// set u_actual_date  of the GlideRecord object to the value
// grObject.u_actual_date = new GlideDateTime(newDT); 
// gs.info(grObject.u_actual_date);

Seems you may need to provide more details about what you're looking for. 

Maddysunil
Kilo Sage

@Alon Grod 

 

// Assuming you have a GlideRecord object named 'gr'
var gr = new GlideRecord('your_table_name');
gr.addQuery('your_condition_field', 'your_condition_value');
gr.query();

if (gr.next()) {
    // Get the current date/time value from the field
    var currentDate = gr.u_actual_date.getGlideObject();
    
    // Set the desired hour (24-hour format)
    var desiredHour = 15; // For example, set to 3 PM
    
    // Set the hour to the desired hour without altering other components
    currentDate.setHours(desiredHour);
    
    // Update the field with the new date/time value
    gr.u_actual_date = currentDate;
    gr.update();
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks