Converting Date to Date/time

nate_weldon1
Kilo Contributor

This is probably a simple one but I'm looking to convert a date to date+time field. I'm starting a service catalog entry for an onboarding request and the start date field is a date type. I want the due date to be the start date, but when because the system is looking for date/time the field does not populate. So I'm guessing I need to convert the date to match the format the system is looking for.

Does anyone have a quick script for this or am I missing a date/time function that's built into the system?

5 REPLIES 5

Mark Stanger
Giga Sage

Variable types can be changed at will, but field types cannot unless the underlying data type at the database level is the same. Unfortunately, date and date/time fields aren't compatible so the only option is to create a new field and try and convert the data over.


Jay_Ford
Kilo Guru

I think I understand the question. You have a date selector on your form and you'd like to use that date as the due date for a task or request item. You can do this in the workflow in an script action. We have a date variable on our form as well. The script below makes our request item due one day after the start date, or if the start date has already passed (please don't ask why I had to account for this) it make the due date 3 days from the current date. It uses the current time at the time of submission to set the time portion of the due date date/time, you could just as easily set it to the end of the business day.



setReqItmVals();

function setReqItmVals(){
current.due_date.setDateNumericValue(startdScript());

}

function startdScript() {

var pd = 1;
var fd = 3;
var answer;
var nowdt = new Packages.com.glide.glideobject.GlideDateTime();
nowdt.setDisplayValue(gs.nowDateTime());
var nowMs = nowdt.getNumericValue();

var theTime = nowdt.toString().split(' ')[1];
//you can just as easily set this to a time manually
//var theTime = '17:00:00'; //for 5pm

var anActualDate = new Packages.com.glide.glideobject.GlideDateTime();
anActualDate.setValue(current.variable_pool.start_date.getGlideObject().getValue() + ' ' + theTime);
var startDate = anActualDate.getNumericValue();
var sdms = startDate;
var pdms = startDate;
pdms -= pd*24*60*60*1000;
var fdms = nowMs;
fdms += fd*24*60*60*1000;

if (sdms < fdms) {
answer = fdms;
}
else{
answer = pdms;
}
return answer;
}


I feel like this is so close to what I need.   But I can't understand the code to adapt it.   I just need to take a value entered in date form as a variable on a request item and write that out to a date time field on the user profile.


Perhaps this can give you a pointer.



Did this in background script to show you what I mean:


Code:


var date = gs.now(); //Variable gets 2016-06-27


gs.log(date);



var dateTime = new GlideDateTime(date);


gs.log(dateTime);



Result:



*** Script: 2016-06-27
*** Script: 2016-06-27 00:00:00


Let me know if you hit a wall.



//Göran