How to set string variable into DateTime field?

Kingstan M
Kilo Sage

Hello,

I have a variable which will process date/time/am (or) pm in a seperate logic and push the calculated time into a var.

e.g., gs.log(patchStart) >> 03-08-2022 6:00 PM

Now i want to set this to a catalog item variable which is in RITM form.

When i try with below code. The value thus being set is either random number or takes +/- 1Hour.

cart.setVariable(item, 'start_timestamp',  patchStart);
 
Any comment for this case?
1 ACCEPTED SOLUTION

Mahendra RC
Mega Sage

Hello Kingstan,

You can try with the below script once to check if you are able to set the String field value into Date/time field

(function () {
	var incoming =  '28-08-2022 02:00 PM';
	var incomingTime = "";
	if (incoming.indexOf("AM") >= 0) 
		incomingTime = incoming.replace(" AM", ":00");
	else
		incomingTime = incoming.replace(" PM", ":00");
	
	var gdt_incoming = new GlideDateTime(incomingTime);
	gs.print(gdt_incoming);
	if (incoming.indexOf("PM") >= 0)
		gdt_incoming.addSeconds(12*60*60) // adding 12 hours if PM to convert in 24 hour time format
	gs.print(gdt_incoming);
	var numericValue = gdt_incoming.getNumericValue();
	
	var gr = new GlideRecord("change_request");
	gr.get("751f2f4097f1d5104cadfb000153af14")
	gr.start_date.setDateNumericValue(numericValue);
	gr.update();
})();

Please mark my respsone as helpful/correct, if it answer your question.

Thanks

 

View solution in original post

4 REPLIES 4

Yousaf
Giga Sage

HI Kingstan,

Please refer to this link and make changes

How to set mm-dd-yyyy value from string to date field

 

Mark Correct or Helpful if it helps.


***Mark Correct or Helpful if it helps.***

I refered that already. It did not help me.

Kingstan M
Kilo Sage

@Abhinay Erra -

Hi, Abhinay Erra.

Requesting your comment on this for me to solve this, Please?

Mahendra RC
Mega Sage

Hello Kingstan,

You can try with the below script once to check if you are able to set the String field value into Date/time field

(function () {
	var incoming =  '28-08-2022 02:00 PM';
	var incomingTime = "";
	if (incoming.indexOf("AM") >= 0) 
		incomingTime = incoming.replace(" AM", ":00");
	else
		incomingTime = incoming.replace(" PM", ":00");
	
	var gdt_incoming = new GlideDateTime(incomingTime);
	gs.print(gdt_incoming);
	if (incoming.indexOf("PM") >= 0)
		gdt_incoming.addSeconds(12*60*60) // adding 12 hours if PM to convert in 24 hour time format
	gs.print(gdt_incoming);
	var numericValue = gdt_incoming.getNumericValue();
	
	var gr = new GlideRecord("change_request");
	gr.get("751f2f4097f1d5104cadfb000153af14")
	gr.start_date.setDateNumericValue(numericValue);
	gr.update();
})();

Please mark my respsone as helpful/correct, if it answer your question.

Thanks