Convert 'Long Date' to HL7 Date 'yyyyMMddhhmmss'

I need to convert a 'Long Date' to HL7 date, and I was trying to do it with some of functions available and not appending each component of the date and time independently.
Input format: 'Mon May 21, 2018 11:52 PM'
Output format: 'yyyyMMddhhmmss'

      // example: Mon May 21, 2018 11:52 PM
      var d1 = new Date("Mon May 21, 2018 11:52 PM");    
      // var d = org.joda.time.format.DateTimeFormat.forPattern('MM/dd/yyyy').parseDateTime(ServDatetime);

      var dt = d1.toLocaleDateString() + " " + d1.toLocaleTimeString().replace(" CDT", "").replace(" PM", "");
      
      var d2 = org.joda.time.format.DateTimeFormat.forPattern('DDD MMM dd, yyyy hh:mm').parseDateTime(dt);    // This line fails !!, not sure how to set this up ??
      
      alert('New Date: ' + d2.toString());
sidwelleAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David S.Commented:
Java and JavaScript are two different languages, Mixing them like that won't work.

The way to output a date in that format in plain/vanilla JS is to do concatenation (aka appending a string), like you mentioned. Otherwise, you'd need a library, like "Moment.js".
sidwelleAuthor Commented:
Can you provide an example how to do in Java using the org.joda methods ?
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Does the fact that you are using the keyword "var" mean that you are using a relatively new version of Java, i.e. Java 10 or newer?

If so, you will have access to the java.time library that is included in the JRE now (actually since Java 8) which essentially replaces joda.time. If this is the case, here is the java.time code to convert between the two formats...

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class JavaTime {
    private static final DateTimeFormatter INPUT_PATTERN = DateTimeFormatter.ofPattern("EEE MMM dd, yyyy hh:mm a");
    private static final DateTimeFormatter OUTPUT_PATTERN = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");

    public static void main(String[] args) {
        LocalDateTime d = LocalDateTime.parse("Mon May 21, 2018 11:52 PM", INPUT_PATTERN);
        System.out.println(d.format(OUTPUT_PATTERN));
    }
}

Open in new window


If you really must, the joda.time version would be almost similar, let me know if you do really need an example.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

sidwelleAuthor Commented:
Here are the working examples that I came up with.
Ex 01 works, but the input pattern must match.
Ex 02 works, and its input friendly. parses almost any usable date.  Does Java 'LocalDateTime' have a parse routine like JS that will just take any date ?  Seems line 19 Shouldn't really be necessary ?

Thank you.

/****************************/
	//Example 01, Works ...
	var d0 = "Mon May 21, 2018 11:52 PM";

	var pIn = java.time.format.DateTimeFormatter.ofPattern("EEE MMM dd, yyyy hh:mm a");
	var pOut = java.time.format.DateTimeFormatter.ofPattern("yyyyMMddHHmmss");

	var d1 = java.time.LocalDateTime.parse(d0, pIn);

	return  d2.format(pOut);

Open in new window

/****************************/
	//Example 02, Works ...
	// var d0 = "Monday May 21, 2018 11:52 PM";	// Works 20180521235200
	// var d0 = "Mon May 21, 2018 11:52 PM";	// Works 20180521235200
	// var d0 = "May 21, 2018 11:52 PM";		// Works 20180521235200
	
	// var d0 = "2018/05/21 11:52 PM";			// Works 20180521235200
	// var d0 = "05/21/2018 11:52 PM";			// Works 20180521235200
	// var d0 = "05/21/2018 23:52";			// Works 20180521235200
	// var d0 = "5/21/2018 23:52";			// Works 20180521235200
	// var d0 = "5/21/2018 23:52:01";			// Works 20180521235201
	// var d0 = "5/21/2018 23";				// Fails
	var d0 = "5/21/2018";					// Works 20180521000000
	 

	var pIn = java.time.format.DateTimeFormatter.ofPattern("MMM dd, yyyy hh:mm:ss a");
	var pOut = java.time.format.DateTimeFormatter.ofPattern("yyyyMMddHHmmss");

	var d1 = new Date(d0);	// <- This is nice because it parses any parsable date.
	var dt = d1.toLocaleDateString() + " " + d1.toLocaleTimeString().replace(" CDT", "")		//'May 21, 2018 11:52:00 PM CDT'

	var d2 = java.time.LocalDateTime.parse(dt, pIn);

	return d2.format(pOut);

Open in new window

/****************************/
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Does Java 'LocalDateTime' have a parse routine like JS that will just take any date ?

No, it doesn't seem to have this functionality. You can use the way you have above, i.e. using the Date() constructor but I would make sure that you test extensively, especially around daylight savings transitions and in different time zones if this code is to be widely distributed. This is because the older Date class has numerous known issues around this, so beware.

The more optimal solution might be to just create a number of different, fixed pattern formatters and just try each one until one successfully parses your input. The code may look a little more cumbersome, but I think the benefits would pay off. You would still want to test pretty well though, particularly around the order of the patterns that you try, and make sure that a date in a different format ISN'T also a valid but different date when applying a different pattern.
sidwelleAuthor Commented:
This code is intended for a single interface and so it won't be distributed across several systems. The problem is that I can't control what format the dates arrive in.  The code above solves my issue and testing shows that it works,  I have not been able to cause it to fail with the data that I am seeing in the files.  As far as a brute force routine to discover what format the data is in, I was assuming that this is what happens inside the JS Date object, so I didn't want to re-invent it.

Using 'var' for JavaScript.

Thanks for the help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.