?
Solved

get date

Posted on 2011-10-16
5
Medium Priority
?
189 Views
Last Modified: 2012-05-12
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>

Open in new window

0
Comment
Question by:XK8ER
5 Comments
 
LVL 15

Accepted Solution

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

Assisted Solution

by:Rajkumar Gs
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>

Open in new window


0
 
LVL 23

Expert Comment

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

Raj
0
 
LVL 84

Assisted Solution

by:Dave Baldwin
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

by:HonorGod
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question