July 17, 2010

Display Date in MM/DD/YYYY format using JavaScript




To display a date in mm/dd/yyyy format in JavaScript, use this script

<script type="text/javascript">
var
currentDt = new Date();
var mm = currentDt.getMonth() + 1;
var dd = currentDt.getDate();
var yyyy = currentDt.getFullYear();
var date = mm + '/' + dd + '/' + yyyy;
alert(date);
</script>

OUTPUT

image

Similarly if you want to always represent the month as double digits, just add a ‘0’ as shown below:

var mm = currentDt.getMonth() + 1;
mm = (mm < 10) ? '0' + mm : mm;

The output now is:

image

If you know of a better way without using any JavaScript frameworks, I would love to hear it! Use the comments section.



'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

2 Responses to "Display Date in MM/DD/YYYY format using JavaScript"
  1. JavaScript Media Player said...
    July 25, 2010 1:38 AM

    very cool & good js tip, thank you very much for sharing.

  2. Muhammad Taqi said...
    August 17, 2010 3:22 AM

    good job !

 

Copyright © 2009-2012 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions