Zero Date Fill Using JavaScript

I want to format the date as follows:

May 02 2013

but for now my formatting is as follows:

May 2 2013

How can I set the size of this type of date to zero so that day to day is something like 02 , not just 2 ?


Here is the code I'm using:

 var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); var d = new Date(); var curr_date = d.getDate(); var curr_month = d.getMonth(); var curr_year = d.getFullYear(); alert( m_names[curr_month] + " " +curr_date + " " + curr_year); 

jsFiddle here

+7
source share
6 answers

You can implement this logic as follows:

 var d = new Date(); var curr_date = ("0" + d.getDate()).slice(-2); var curr_month = d.getMonth(); var curr_year = d.getFullYear(); console.log( m_names[curr_month] + " " +curr_date + " " + curr_year); 

Fiddle


UPDATES - (October 1, 2017)

EcmaScript 2017 or ES8 introduced two new String prototype methods: padStart() and padEnd() . We can add extra spaces or dashes (or any other character) before or after the line. We can use it here as:

 var curr_date = d.getDate().toString().padStart(2,0); 

(This syntax may be shorter if we don't need to convert d.getDate () to a string, since padStart only works on a string)

Demo:

 var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); var d = new Date('10/1/2017'); var curr_date = d.getDate().toString().padStart(2, 0); var curr_month = d.getMonth(); var curr_year = d.getFullYear(); var formatted_date = m_names[curr_month] + " " + curr_date + " " + curr_year; console.log(formatted_date); 
+17
source

This is what you could. Just see if the date is greater than 9. If so, use if you don't add a leading zero

 var curr_date = d.getDate(); curr_date = curr_date > 9 ? curr_date : "0" + curr_date; 
+4
source

Decision

Make sure curr_date lower than 9. If yes, add "0" + curr_date .

So you do it inline as below:

 curr_date = (curr_date <= 9 ? "0" : "") + curr_date 

Or directly in the alert:

 alert( m_names[curr_month] + " " + (curr_date <= 9 ? "0" : "") + curr_date + " " + curr_year); 

Description

As you can see, I used ?: .

In computer programming, ?: Is a ternary operator that is part of the syntax of a basic conditional expression in several programming languages. It is usually called a conditional statement, inline if (iif) or triple if.

How it works?

It's simple! Take a look:

 condition ? value_if_true : value_if_false` 

or

 variable1 === variable2 ? value_if_true : value_if_false 

See the updated FIDDLE .

+2
source

In the current version of the Chrome browser (v28.0.1496.0), each Number object has a function toPaddedString(n) .

 Number(16).toPaddedString(4) "0016" 

As for the rather narrow browser, but I doubt it will bring you much benefit. This may be part of the production version of Chrome, but right now I do not have this version. This feature is not part of EcmaScript 5.1, so itโ€™s best not to rely on it. However, this is not [native code] , so ...

 Number(1).toPaddedString function g(k,j){var i=this.toString(j||10);return"0".times(ki.length)+i} 

So, to use this function in any browser, include this line of code:

 Number.prototype.toPaddedString = function g(k,j){var i=this.toString(j||10);return"0".times(ki.length)+i}; 

Then your month can be supplemented, for example:

 var curr_month = d.getMonth().toPaddedString(2); 
+1
source
 $("#cuentapendiente").focusout(function(){ if($(this).val()!=""){ $(this).val(zeroFill($(this).get(0).value,6)) } } 
0
source

This solution in coffeescript (wrapped in an eco file) worked for me

 <% zero_pad = (x) -> if x < 10 then '0'+x else ''+x %> <% d = new Date() %> <% m = d.getFullYear() %> <% m += "-"+zero_pad (d.getMonth()+1) %> <% m += "-"+zero_pad d.getDate() %> <input type="date" name="date" width="20" value="<%- m %>"> 
-one
source

All Articles