Ad

0 Pad Day And Month In A Date String Using JavaScript

- 1 answer

Using JavaScript is there a way to take a Date string like this 9/17/2014 and modify it so that the day and month values are always padded with a 0 if they are single digit in length?

The catch is that the delimiter / can be anything. Generally it would be - or / so if it only looked for those 2 would be fine to.

Also the order of the day, month, and year can be in any position.

My end goal is to take a date string and make it so that the Date portion is always 10 characters in length so that I can then grab the remaining characters is they exist and always assume they are the Time portion of the DateTime string.

If I get a string like this though 9/17/2014 then the Date portion is only 9 long and if the day was single digit like 9/7/2014 then it would only be 8 long.


I am working on this JavaScript function to convert a Date string into a JS Date object. It allows any style delimiter and any order for the day, month, and year parts.

My goal is to modify it to also allow Date strings that can have a Time after the Date part and parse them into time pieces.

If the string has the Time parts, I will assume time delimiter will always be :.

The separator between the Date and a Time in the string could be either:

  1. space like 04/04/2016 12:23:12
  2. T like 04/04/2016T12:23:12

Also the time may or may not have the ending am or pm.

My issue at the moment is the Date part sometimes not being the 10 character length so if I could auto 0 pad single digit day and months it might help in the end goal.

Any help with any of this goal appreciated though, thanks


  // Convert Date String into JS Date Object
  // Can parse multiple Date formats
  // stringToDate("17/9/2014", "dd/MM/yyyy", "/");
  // stringToDate("9/17/2014", "mm/dd/yyyy", "/");
  // stringToDate("9-17-2014", "mm-dd-yyyy", "-");
  stringToDate: function(d, _format, _delimiter) {
    if((d instanceof Date)){
      return d;
    }
    if(d.length > 10){
      // it might have a Time string at the end
      var dateString = d.substring(0, 10);
      console.log(dateString);
    }

    var formatLowerCase = _format.toLowerCase();
    var formatItems = formatLowerCase.split(_delimiter);
    var dateItems = d.split(_delimiter);
    var monthIndex = formatItems.indexOf("mm");
    var dayIndex = formatItems.indexOf("dd");
    var yearIndex = formatItems.indexOf("yyyy");
    var month = parseInt(dateItems[monthIndex]);
    month -= 1;
    var formatedDate = new Date(dateItems[yearIndex], month, dateItems[dayIndex]);
    return formatedDate;
  },
Ad

Answer

You can use an iterating function to pad out the date:

function pad(str) {
  if (str.length === 2) return str;
  return pad('0' + str);
}

And use a mediating function to separate the date components and pad them where necessary:

function padDate(str, delimiter) {
  return str.split(delimiter).map(function (el) {
    if (el.length < 2) return pad(el);
    return el;
  }).join(delimiter);
}

Declare what you want the delimiter to be as a parameter to the padDate function.

padDate('9/17/2014', '/'); // 09/17/2014
padDate('19-7-2014', '-'); // 19-07-2014

DEMO

Ad
source: stackoverflow.com
Ad