28

I want to get today's date in the format of mm-dd-yyyy

I am using var currentDate = new Date(); document.write(currentDate);

I can't figure out how to format it.

I saw the examples var currentTime = new Date(YY, mm, dd); and currentTime.format("mm/dd/YY");

Both of which don't work

I finally got a properly formatted date using

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;//January is 0!`

var yyyy = today.getFullYear();
if(dd<10){dd='0'+dd}
if(mm<10){mm='0'+mm}
var today = mm+'/'+dd+'/'+yyyy;
document.write(today);'`

This seems very complex for such a simple task.

Is there a better way to get today's date in dd/mm/yyyy?

kristian
  • 22,731
  • 8
  • 50
  • 78
Samuel Meddows
  • 36,162
  • 12
  • 38
  • 36
  • I'd do it manually `var d = new Date(); var s = new String(); s = (d.getHours()+"-"+d.getMinutes()+"-"+d.getSeconds()+" "+d.getDate()+"-"+d.getMonth()+"-"+d.getFullYear()).toString();`. Reference: http://www.w3schools.com/jsref/jsref_obj_date.asp – Nav Feb 01 '15 at 04:53

8 Answers8

24

Unfortunately there is no better way, but instead of reinventing the wheel, you could use a library to deal with parsing and formatting dates: Datejs

<plug class="shameless">

Or, if you find format specifiers ugly and hard to decipher, here's a concise formatting implementation that allows you to use human-readable format specifiers (namely, the Date instance getters themselves):

date.format("{Month:2}-{Date:2}-{FullYear}"); // mm-dd-yyyy

</plug>

Ates Goral
  • 137,716
  • 26
  • 137
  • 190
18
var today = new Date();

var strDate = 'Y-m-d'
  .replace('Y', today.getFullYear())
  .replace('m', today.getMonth()+1)
  .replace('d', today.getDate());
mrded
  • 4,674
  • 2
  • 34
  • 36
4

Simple answer is no. Thats the only way to do it that I know of. You can probably wrap into a function that you can reuse many times.

Victor
  • 4,721
  • 1
  • 26
  • 31
1

date.js is what you need. For example, snippet below is to convert a date to string as Java style

new Date().toString('M/d/yyyy')
Bao Le
  • 16,643
  • 9
  • 65
  • 68
1
function dateNow(splinter){
  var set = new Date(); 
  var getDate = set.getDate().toString();
  if (getDate.length == 1){ //example if 1 change to 01
   getDate = "0"+getDate;
  }
  var getMonth = (set.getMonth()+1).toString();
  if (getMonth.length == 1){
   getMonth = "0"+getMonth;
  }
  var getYear = set.getFullYear().toString();
  var dateNow = getMonth +splinter+ getDate +splinter+ getYear; //today
  return dateNow;
}

format this function is mm dd yyyy and the dividing you can choice and replace if you want... for example dateNow("/") you will get 12/12/2014

Widada
  • 575
  • 2
  • 4
  • 17
0

There is nothing built in, but consider using this if you are already using jQuery (and if not, then you should consider that as well!)

RichardTheKiwi
  • 105,798
  • 26
  • 196
  • 262
-2
(new Date()).format("MM-dd-yyyy")

N.B. month is "MM" not "mm"

WantToDo
  • 401
  • 1
  • 5
  • 11
  • 1
    Your description is not clear. Also, there are a lot of really good answers to this question already. Does your answer contribute anything? If so, please elaborate in your answer. – Suever Apr 10 '16 at 15:56
-4
function appendZeros(value,digits){
    var c= 1;
    initValue = value;
    for(i=0;i<digits-1;i++){
        c = c*10;
        if( initValue < c ){
            value = '0' + value;
        }
    }
    return value;
}