The CreatorCon Call for Content is officially open! Get started here.

Splitting DateTime fields

yundlu316
Kilo Guru

Hi, I am trying to create a widget that has a uib-datepicker and am having issues splitting date/time fields to separate date and time elements. 

The only thing I got to work so far looks like this:

	var avail =  new GlideRecord(appt_avail);
		avail.addQuery('active', 'true');
		avail.addQuery('appointment', input.Appt.sys_ID);
		avail.query();
		while(avail.next()) {
			var appt = avail.appointment.getRefRecord();
			var loc = avail.appointment_location.getRefRecord();
			var contact = avail.contact.getRefRecord();
			var appt_obj = {};
			
			appt_obj.start_date_time = avail.getDisplayValue('start_date_time');
			appt_obj.start_date = returnDate(appt_obj.start_date_time).toLocaleDateString();
			appt_obj.start_time = returnDate(appt_obj.start_date_time).toLocaleTimeString();
			data.avail.push(appt_obj);
		}
		gs.addInfoMessage(data.avail[0].start_date_time);
		gs.addInfoMessage(data.avail[0].start_date);
		gs.addInfoMessage(data.avail[0].start_time);
	}


	function returnDate(dateTime) {
		var dateTime2 = dateTime.split(" ");//dateTime[0] = date, dateTime[1] = time
		var date = dateTime2[0].split("-");
		var time = dateTime2[1].split(":");
		return new Date(date[0], (date[1]-1), date[2], time[0], time[1], time[2]);
	
	}

 I am using toLocaleDateString and toLocaleTimeString, which produces separate Date and Time in my infomessages, however, the timezone is incorrect.  I've checked my user profile and system properties to make sure both are in US/Eastern timezones, but it still returns with PDT.  

My question is 1) is this the correct way to split date/time fields and 2) if it is, how do I get the timezone to show up correctly?

 

Thanks!

1 ACCEPTED SOLUTION

jarodm
Mega Guru

 

var myDate = new GlideDateTime();

var dateonly = myDate.getDate().toString();
gs.info('dateonly:'+dateonly);
		
var gettime = myDate.getTime();
gs.info('gettime:'+gettime);

var timeOnly = gettime.getByFormat('hh:mm:ss').toString();
gs.info('timeonly:'+timeOnly);

Try some combination of the above code. I try and use this method instead of the array method.

View solution in original post

3 REPLIES 3

jarodm
Mega Guru

 

var myDate = new GlideDateTime();

var dateonly = myDate.getDate().toString();
gs.info('dateonly:'+dateonly);
		
var gettime = myDate.getTime();
gs.info('gettime:'+gettime);

var timeOnly = gettime.getByFormat('hh:mm:ss').toString();
gs.info('timeonly:'+timeOnly);

Try some combination of the above code. I try and use this method instead of the array method.

//Great Scott! -- This is heavy!

var startTime = source.u_opened + '';
var tz = Packages.java.util.TimeZone.getTimeZone('GMT');
var OUTATIME = new GlideDateTime(); 
OUTATIME.setTZ(tz);
OUTATIME.setValue(startTime);

var utz = gs.getSession().getTimeZone();
OUTATIME.setTZ(utz);
var timeZoneOffSet = OUTATIME.getTZOffset();  
OUTATIME.setNumericValue(OUTATIME.getNumericValue() + timeZoneOffSet);  

//gs.info('tmap returning time:'+gdt+' with offset of:'+timeZoneOffSet);
return OUTATIME;

 

Here is a sample from an old transform map script that I reference quite a bit for manual time zone handling. 

thank you, this is exactly what I needed!