How to Parse Free-Form (Natural Language) Date & Time Input using JavaScript

From an end user perspective, it can get a little frustrating at times entering date & time info into a website. That’s because there are a lot of errors that can occur with improperly formatted data, which means you’ll often get an error message for even the slightest mistakes in your string. Thankfully, you can remove some of that headache with our free-form date & time parser API. It’ll take an input date & time in natural language form and return that data in a structured format (to the greatest extent possible), returning information structured like so:

Sample Entry: "January 12th, 2001 at 10 am"
{
"Successful": true,
"ParsedDateResult": "2001-01-12T10:00:00",
"Year": 2001,
"Month": 1,
"Day": 12,
"Hour": 10,
"Minute": 0,
"Second": 0,
"DayOfWeek": "Friday"
}

You can take advantage of this API easily using code examples provided below in JavaScript. First, however, you’ll need to register a free account on our website to get the necessary API key (free accounts will provide 800 API calls per month with zero additional commitments).

To structure your call using JavaScript’s built-in XHR feature, use the below snippet:

var data = JSON.stringify({
"RawDateTimeInput": "<string>"
});
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function() {
if(this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("POST", "https://api.cloudmersive.com/validate/date-time/parse/date-time/natural-language");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Apikey", "YOUR-API-KEY-HERE");
xhr.send(data);

To first install jQuery instead, run the below command:

bower install jquery

And then use the below code to structure your call:

var settings = {
"url": "https://api.cloudmersive.com/validate/date-time/parse/date-time/natural-language",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/json",
"Apikey": "YOUR-API-KEY-HERE"
},
"data": JSON.stringify({
"RawDateTimeInput": "<string>"
}),
};
$.ajax(settings).done(function (response) {
console.log(response);
});

With that, you’re all done!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cloudmersive

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.