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?

1
  • 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: w3schools.com/jsref/jsref_obj_date.asp Commented Feb 1, 2015 at 4:53

8 Answers 8

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>

Sign up to request clarification or add additional context in comments.

2 Comments

Nice library, came in handy.
Another useful javascript library is momentjs. momentjs.com You can format the date as follow. moment().format('MM-DD-YYYY')
19
var today = new Date();

var strDate = 'Y-m-d'
  .replace('Y', today.getFullYear())
  .replace('m', today.getMonth()+1)
  .replace('d', today.getDate());

Comments

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.

1 Comment

thought that may be the case. Thanks mate
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')

Comments

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

Comments

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!)

Comments

-3
(new Date()).format("MM-dd-yyyy")

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

1 Comment

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.
-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;
}

1 Comment

Perhaps this was meant for another question?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.