Solved

# get date

Posted on 2011-10-16
Medium Priority
189 Views
hello there,
this code is working fine but for some reason when that date is between 1st and 9th of the month it wont give day 01 or day 05...
it just gives 1 or 5.. how can I make it so that it always returns two digits for the day and month?
``````<script type="text/javascript">

var curdate = new Date();
var year = curdate.getFullYear();
var month = curdate.getMonth() + 1;
var day = curdate.getDate();

</script>
``````
0
Question by:XK8ER

LVL 15

Accepted Solution

Eyal earned 668 total points
ID: 36975201
something like this
var month = ('0'+(curdate.getMonth()+1)).slice(-2)
0

LVL 23

Assisted Solution

Rajkumar Gs earned 668 total points
ID: 36975205
Try this code. Check 'day' length. If it is one, append zero to left

``````<script type="text/javascript">

var curdate = new Date();
var year = curdate.getFullYear();
var month = curdate.getMonth() + 1;
var day = curdate.getDate();
if (day.length() == 1)
day = '0' + day;
</script>
``````

0

LVL 23

Expert Comment

ID: 36975206
If works, use the same logic for month as well

Raj
0

LVL 84

Assisted Solution

Dave Baldwin earned 664 total points
ID: 36975229
I've always had to add the leading zero.  I don't know of any javascript date function that will supply it.  http://www.w3schools.com/jsref/jsref_obj_date.asp
0

LVL 41

Expert Comment

ID: 36975912
You might want to take a look at this article: Can I have a date?

http://www.experts-exchange.com/A_484.html
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
###### Suggested Courses
Course of the Month16 days, 23 hours left to enroll