0

I need your help,

I can't seem to find any other help on this on the internet, because it seems its either one way ot the other. What I would like to be able to do is to create a combined, two-fold javascript function that would convert a long date string into the mm-dd-yyyy format, and when the same function is called again with no string specified to convert, to just return todays date in mm-dd-yyyy format.

Example:

getDate(Fri May 22 2015 13:32:25 GMT-0400)

would return: 05-22-2015

getDate()

would return today's date of 05-23-2015

BobbyJones
  • 1,331
  • 1
  • 26
  • 44

3 Answers3

0

Hi this should do the trick

FORMAT: mm-dd-yyyy

function addZeroIfRequired(dayOrMonth) {
    return dayOrMonth > 9 ? dayOrMonth : "0"+dayOrMonth;
}

function getDate(dateString) {
   var date = dateString ? new Date(dateString) : new Date();
   return addZeroIfRequired((date.getUTCMonth()+1)) + "-" + 
          addZeroIfRequired(date.getDate())+  "-" + 
          date.getUTCFullYear();
}

console.log(getDate()); // 05-23-2015
console.log(getDate("Fri May 22 2015 13:32:25 GMT-0400")); 05-22-2015

NOTE: the +1 after the getUTCMonth().

JSFiddle. Open the console to see the result. https://jsfiddle.net/wt79yLo0/2/

ISO FORMAT: yyyy-mm-dd

Just in case someone is interested in the opposite format, the code would be much nicer and neater:

function getDate(dateString) {
   var date = dateString ? new Date(dateString) : new Date();
   return date.toISOString().substring(0, 10);
}

console.log(getDate());
console.log(getDate("Fri May 22 2015 13:32:25 GMT-0400"));

https://jsfiddle.net/wt79yLo0/

Giuseppe Pes
  • 7,772
  • 3
  • 52
  • 90
  • Thanks, I tried it but it puts the date format in 2015-05-23 (yyyy-mm-dd) I need mm-dd-yyyy – BobbyJones May 23 '15 at 18:49
  • It is not guaranteed to work. You are passing a non-standard date format to Date which is actually supposed to accept "A string representing an RFC2822 or ISO 8601 date (other formats may be used, but results may be unexpected)." ([ref](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse)) – Salman A May 23 '15 at 20:26
0

With getDate(), getMonth() and getFullYear(). You have to set a "0" before the months and days which are < 10. GetMonth() starts with 0, therefore (getMonth() + 1).

function getFormattedDate() {
    var date = new Date();
    var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
    var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1);
    var year = date.getFullYear();
    var formattedDate = day + "-" + month + "-" + year;
    return formattedDate;
}

console.log(getFormattedDate());

Demo

0

First I would recommend a very powerful library for JS called Moment.js which solves all this kind of problems.

But if you only want a snippet, here is my proposal:

function twoDigits(num) { 
    return ("0" + num).substr(-2); 
}

function getFormattedDateDMY(date, separator) {
    var day = twoDigits(date.getDate());
    var month = twoDigits(date.getMonth());
    var year = date.getFullYear();
    return [day,month,year].join(separator);
}

function getFormattedDateMDY(date, separator) {
    var day = twoDigits(date.getDate());
    var month = twoDigits(date.getMonth());
    var year = date.getFullYear();
    return [month,day,year].join(separator);
}

console.log(getFormattedDateDMY(new Date(), "-"));  //23-04-2015
console.log(getFormattedDateMDY(new Date(), "-"));  //04-23-2015
Jorgeblom
  • 3,330
  • 1
  • 23
  • 24