• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

get date

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
XK8ER
Asked:
XK8ER
3 Solutions
 
EyalCommented:
something like this
var month = ('0'+(curdate.getMonth()+1)).slice(-2)
0
 
Rajkumar GsSoftware EngineerCommented:
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
 
Rajkumar GsSoftware EngineerCommented:
If works, use the same logic for month as well

Raj
0
 
Dave BaldwinFixer of ProblemsCommented:
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
 
HonorGodSoftware EngineerCommented:
You might want to take a look at this article: Can I have a date?

http://www.experts-exchange.com/A_484.html
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now