How to convert date format
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2014 05:45 AM
Hello all,
I have an inbound action which reads content of the email and populates a table with the information from the email body.
My issues is that the email contains long format date e.g. 24th March 2014 at 23:59hrs and I need to convert this to short format dd/mm/yyyy so it can populate date field on the form.
Appreciate any help with this.
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2014 07:13 AM
There's not really another way to do this, is the date inputted by users and subject to change? if it is then it's going to be extremely tricky to write a code to cater for different formats etc.
If it's writted by a program, i.e. outlook or some other app then it should always be in the same format and you can find some JS or Regex to convert that string into an actual date/time format.
thanks,
Marc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2014 07:25 AM
Yes exactly where i was going with this... if it is entered by the user then there is too much room for different formats
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-24-2014 08:10 AM
Bit late, but after I had to do some work on a regex finding and changing a date format, the core of a script for this was made - It has taken me a little while to refind this thread.
this script will find and convert a date if if is in the format you said, so
- 1 or 2 digits for the date
- then either rd, th or st
- a space then the month in 3 characters
- a space then the year
as others have said,if the user is typing this in, it may fail as it cannot work with typo's or incorrect spacing
No validation on the rd/th/st being correct as it is not needed when adding to a date field
This will also grab the time if you need it, but I have not done anything with that
this script is currently using a number of IF statement to move the month to a numerical value
I have select a switch / case option - but when I run this as a Background Script it takes ages using that method
It will return 0 if there is no date found, otherwise it is yyyy-mm-dd
var str = 'asdf asdf asdf asdf asdf asdf 23rd Feb 2014 at 09:15hrs asdf asdf asdf asdf a';
gs.print(findDate(str));
function findDate(inString)
{
var regex = /\d{1,2}(st|rd|th)\s{1}(Jan|Feb|Mar|Apr|May|Jun|Jul|Apr|Sep|Oct|Nov|Dec)\s{1}\d{4}\s{1}(at)\s{1}\d{1,2}:\d{1,2}(hrs)/;
// Run a match and see if we get a null or a match
// null indicates no match / date
// If we match, get the date so we can work it out, otherwise do not process and dates
if (inString.match(regex) !== null)
{
var retdate = inString.match(regex);
var dArr = '';
dArr = retdate[0].split(' ');
// get just the date from the first part (we get rid of rd,st or th)
var regex2 = /\d{1,2}/;
var rdate = dArr[0].match(regex2)
var rmonth = '';
if (dArr[1].toLowerCase() == 'jan')
rmonth = '01';
if (dArr[1].toLowerCase() == 'feb')
rmonth = '02';
if (dArr[1].toLowerCase() == 'mar')
rmonth = '03';
if (dArr[1].toLowerCase() == 'apr')
rmonth = '04';
if (dArr[1].toLowerCase() == 'may')
rmonth = '05';
if (dArr[1].toLowerCase() == 'jun')
rmonth = '06';
if (dArr[1].toLowerCase() == 'jul')
rmonth = '07';
if (dArr[1].toLowerCase() == 'aug')
rmonth = '08';
if (dArr[1].toLowerCase() == 'sep')
rmonth = '09';
if (dArr[1].toLowerCase() == 'oct')
rmonth = '10';
if (dArr[1].toLowerCase() == 'nov')
rmonth = '11';
if (dArr[1].toLowerCase() == 'dec')
rmonth = '12';
/*
switch(dArr[1].toLowerCase())
{
case 'jan':
rmonth = '01'
break;
case 'feb':
rmonth = '02'
break;
case 'mar':
rmonth = '03'
break;
case 'apr':
rmonth = '04'
break;
case 'may':
rmonth = '05'
break;
case 'jun':
rmonth = '06'
break;
case 'jul':
rmonth = '07'
break;
case 'aug':
rmonth = '08'
break;
case 'sep':
rmonth = '09'
break;
case 'oct':
rmonth = '10'
break;
case 'nov':
rmonth = '11'
break;
case 'dec':
rmonth = '12'
break;
}
*/
// return dArr[2]+'-'+dArr[1]+'-'+dArr[0];
return dArr[2]+'-'+rmonth+'-'+rdate[0];
}
else
{
return '0';
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-01-2017 09:37 PM
Hey Julian, I've done somewhat similar implementation to auto-populate 2 fields - 1 GlideDate field and another a GlideDateTime field. I'm using an onChange client script to set these 2 fields. Working as expected.
The issue here is both these fields are coming up in yyyy-mm-dd formats. But I need the format to be in dd/MM/yyyy. Tried using many methods including the getByFormat() in the script include but in vain...
Anything that you can help me here mate?
Cheers
Suhas

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-01-2017 10:48 PM
Please check if this helps: ServiceNow KB: Service portal not showing the specified date format (KB0597771)