Skip to content Skip to sidebar Skip to footer

How To Convert ISO Date And Time Format To "DD Mon YYYY Hh:mm:ss"?

I have a variable like this, var date = '2016-04-07T03:03:03Z'; How do I convert it to a time format like this 6 Apr 2016, 8:03:03 PMin the local timezone using JavaScript/jQuery?

Solution 1:

Solution using Date.prototype.toLocaleDateString() function:

var date_str = "2016-04-07T03:03:03Z",
    options = { year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit'},
    formatted = (new Date(date_str)).toLocaleDateString('en-US', options),
    date_parts = formatted.substring(0, formatted.indexOf(",")).split(" ").reverse().join(" ");

var formatted_date = date_parts + formatted.substr(formatted.indexOf(",") + 1);

console.log(formatted_date);

The output will look like the following(according to your locale):

7 Apr 2016, 6:03:03 AM

Solution 2:

Edit 2021-02-26: Consider using a more modern library such as https://date-fns.org/

Original:

As you want to parse it with a timezone and then format the output, I'd strongly suggest using Moment.js which is a well used library for time and date manipulation:

The code would look something like this:

var date = "2016-04-07T03:03:03Z";
console.log(moment(date).format('D MMM YYYY, h:mm:ss A')); 
// "7 Apr 2016, 5:03:03 AM"

Solution 3:

Try this:

var date = "2016-04-07T03:03:03Z";
var myDate = new Date(date);

console.log(myDate);

Thu Apr 07 2016 05:03:03 GMT+0200 (W. Europe Daylight Time)

The new Date(date) convert in the local timezone

If you want more control over the value you can also format the date, see this article for more info


Solution 4:

I've tried something that might help you.

EDIT: Updated my code snippet to format military time into standard time

function formatDate ( today ) {
  var newDateItems = new Array();
  var dateItems = String(today).split(" ");
  dateItems.forEach(function(item, index){
  	if (index > 0 && index < 5) {
      if (index == 4){
        item = getStandardTime(item);
      }
      newDateItems.push(item);
    }
  });
  return newDateItems.join(" ");
}
//To format military time into standard time
function getStandardTime( time ) {
  time = time.split(":");
  var hh = Number(time[0]);
  var mm = Number(time[1]);
  var ss = Number(time[2]);
  var timeValue = "";
  if (hh > 12) timeValue += hh - 12;
  else timeValue += hh;
  
  if (mm < 10) timeValue += ":0" + mm;
  else timeValue += ":" + mm
 	
  if (ss < 10) timeValue += ":0" + ss;
  else timeValue += ":" + ss
  
  timeValue += (hh >= 12) ? " PM" : " AM"; 
  return timeValue
  
}
var dateToday = new Date();
document.write(formatDate(dateToday));

Solution 5:

Here is a function that also uses type-script, and also adds 0 in front of the minute and hour if it is one digit.

function convertISODateToTimeFormat(ISODate: string) {
      const newDateObj = new Date(ISODate);
      const toMonth = newDateObj.getMonth() + 1;
      const toYear = newDateObj.getFullYear();
      const toDate = newDateObj.getDate();
      const toHours = newDateObj.getHours();
      const toHoursProcessed = (toHours < 10 ? '0' : '') + toHours;
      const toMin = newDateObj.getMinutes();
      const toMinProcessed = (toMin < 10 ? '0' : '') + toMin;
      const dateTemplate = `${toDate}.${toMonth}.${toYear} ${toHoursProcessed}:${toMinProcessed}`;
      // console.log(dateTemplate)
      return dateTemplate;
    }
    convertISODateToTimeFormat('2019-08-07T02:01:49.499Z')

Post a Comment for "How To Convert ISO Date And Time Format To "DD Mon YYYY Hh:mm:ss"?"