Comments on: Parsing the Date and Time Sections from a Date/Time field https://servicenowguru.com/scripting/parsing-date-time-sections-from-datetime-field/ ServiceNow Consulting Scripting Administration Development Wed, 06 Mar 2024 20:41:44 +0000 hourly 1 https://wordpress.org/?v=6.8.2 By: ND https://servicenowguru.com/scripting/parsing-date-time-sections-from-datetime-field/#comment-6212 Mon, 30 Dec 2013 21:44:17 +0000 https://servicenowguru.wpengine.com/?p=634#comment-6212 Hi.

Thanks for the useful code, I checked and it’s working fine in Business rule.

But when I try to implement same in Script include, it’s not working. I want to create a script include function and call it in client script.

regards,
ND

]]>
By: valor https://servicenowguru.com/scripting/parsing-date-time-sections-from-datetime-field/#comment-6211 Tue, 11 Sep 2012 01:36:59 +0000 https://servicenowguru.wpengine.com/?p=634#comment-6211 In reply to Anonymous.

Thanks, Amit!
I made it a bit more reusable for people:
[code]
formatDate = function (dateDisplay, desiredFormat){
if (!desiredFormat){
desiredFormat = “dd-MMM-yyyy”;
}

// build Java date objects
var inputformat = gs.getProperty(‘glide.sys.date_format’) + ”;
var jDateFormat = new Packages.java.text.SimpleDateFormat(inputformat);
var jDesiredDateFormat = new Packages.java.text.SimpleDateFormat(desiredFormat);

// assume “dateDisplay” is in current user’s TZ
jDateFormat.setTimeZone(Packages.java.util.TimeZone.getTimeZone(gs.getUser().getTZ()));
// assume output will be in system TZ, regardless of user
jDesiredDateFormat.setTimeZone(Packages.java.util.TimeZone.getTimeZone(gs.getProperty(‘glide.sys.default.tz’)));

// Create Java date object based off of input
var jInDate = jDateFormat.parse(dateDisplay);
// Create converted Java date obj
var jOutDate = jDesiredDateFormat.format(jInDate);

// return a formatted String-primitive
return jOutDate + ”;
}[/code]

]]>
By: Anonymous https://servicenowguru.com/scripting/parsing-date-time-sections-from-datetime-field/#comment-6210 Sat, 16 Jan 2010 09:10:13 +0000 https://servicenowguru.wpengine.com/?p=634#comment-6210 In my environment while working with dates I prefer to use date/time format stored in property. Below script is used in one of my script includes which converts expected start date into date format required by the underlying system.

 
// This Function takes the expected start date value from change_task and convert it as per bladelogic format 
formatBLDeployDate: function(deployDate){ 
// Getting date and time format as per system properties 
var inputformat = gs.getProperty('glide.sys.date_format') + ' ' + gs.getProperty('glide.sys.time_format'); 
var lv_parser = new java.text.SimpleDateFormat(inputformat); 
// System stores date in UTC format. 
lv_parser.setTimeZone(java.util.TimeZone.getTimeZone('UTC')); 
// Date format for Bladelogic this date is split latter for date and time 
var lv_formatter = new java.text.SimpleDateFormat('MM/dd/yyyy HH:mm:ss'); 
// Setting system time zone which is US/Eastern 
lv_formatter.setTimeZone(java.util.TimeZone.getTimeZone(gs.getProperty('glide.sys.default.tz'))); 
// Parse the date and convert it into Date object 
var lv_localDate = lv_parser.parse(deployDate); 
// Convert date in local time zone format 
lv_dateFormateInLocalTimeZone = lv_formatter.format(lv_localDate); 
gs.log('Formated Date for Bl --&> '+ lv_dateFormateInLocalTimeZone); 
return lv_dateFormateInLocalTimeZone; 
 
}, 
]]>