web stats
Mirth Community - View Single Post - Mirth Tools: User defined functions
View Single Post
  #27  
Old 12-06-2013, 02:20 PM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,072
narupley is on a distinguished road
Default Convert Date from Multiple Formats/Sources

Quote:
UPDATE: I've created a public GitHub repository to track these example channels, code templates, scripts, or whatever else!

https://github.com/nextgenhealthcare/connect-examples

To start with I only added the ones I wrote, because I didn't want to presume and add code from others without their explicit permission. Pull requests welcome!
convertDate: Parses a date (String, Date, DateTime, or Number) into a string using an array of possible input patterns and the specified output pattern.

This is useful if you want to convert/standardize an incoming date, but you don't know how it will be formatted. The code comes with an array of possible patterns, and if there is a pattern you encounter that isn't handled, you can simply add it to the list.

Parameters:
  • date: The object to convert. The object may be a String, Date, DateTime, or Number (or really any object, as long as its string representation is some formatted date).
  • outpattern: The pattern to use when converting the date to the output string.

Example:
  • Standardize the message date/time to a specific pattern, no matter what pattern it comes in as:
    Code:
    msg['MSH']['MSH.7']['MSH.7.1'] = convertDate(msg['MSH']['MSH.7']['MSH.7.1'].toString(), 'yyyyMMddHHmmss');

The Code:

Code:
var patterns = [
	'yyyy-MM-dd HH:mm:ss:SSS',
	'yyyy-MM-dd HH:mm:ss.SSS',
	'yyyy-MM-dd HH:mm:ss',
	'yyyy-MM-dd HH:mm',
	'EEE MMM dd HH:mm:ss:SSS zzz yyyy',
	'EEE MMM dd HH:mm:ss.SSS zzz yyyy',
	'EEE MMM dd HH:mm:ss zzz yyyy',
	'EEE MMM dd zzz yyyy',
	'dd-MMM-yyyy HH:mm:ss:SSS',
	'dd-MMM-yyyy HH:mm:ss.SSS',
	'dd-MMM-yyyy HH:mm:ss',
	'yyyy-MM-dd',
	'yyyy MM dd',
	'yyyy.MM.dd',
	'MM-dd-yyyy',
	'MM dd yyyy',
	'MM.dd.yyyy',
	'HH:mm:ss:SSS',
	'HH:mm:ss.SSS',
	'HH:mm:ss',
	'yyyyMMddHHmmssSSS',
	'yyyyMMddHHmmss',
	'yyyyMMddHHmm',
	'hh:mm aa'];

var formatters = [];
for each (pattern in patterns) {
	formatters.push(org.joda.time.format.DateTimeFormat.forPattern(pattern));
}

function convertDate(date, outpattern) {
	var instant = 0;
	if (typeof date == 'number' || date instanceof java.lang.Number)
		instant = new Number(date);
	else if (date instanceof Date || date instanceof java.util.Date)
		instant = date.getTime();
	else if (date instanceof org.joda.time.ReadableInstant)
		instant = date.getMillis();
	else {
		for each (formatter in formatters) {
			try {
				instant = formatter.parseMillis(new String(date));
				break;
			} catch(e) {}
		}
	}

	return org.joda.time.format.DateTimeFormat.forPattern(outpattern).print(instant);
}
Attached Files
File Type: xml convertDate.xml (1.8 KB, 226 views)
__________________
Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

Nicholas Rupley
Work: 949-237-6069
Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


- How do I foo?
- You just bar.

Last edited by narupley; 06-08-2018 at 11:36 AM.
Reply With Quote