How to Get Month and Date of JavaScript in 2 Digit Format

How do I get Month and Date of JavaScript in 2 digit format?

("0" + this.getDate()).slice(-2)

for the date, and similar:

("0" + (this.getMonth() + 1)).slice(-2)

for the month.

How to get two digit month and days

We can create a simple function that takes in a value and based on its length, pre-pends a zero to it. Then, take your date portions and run them through the function:

(function ($) {      $(document).ready(function() {    var now = new Date();    var created = now.getFullYear() + '-' +                   fixDigit(now.getMonth() + 1) +                   '-' + fixDigit(now.getDate());    $('#created').val(created);  });        // Utility function to prepend zeros to single digits:  function fixDigit(val){    return val.toString().length === 1 ? "0" + val : val;  }})(jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><input type="text" id="created">

how to get my month and date in two digit formats

Format the month into 2 digit format using the following method.

mm = ("0" + mm).slice(-2);
var someFormattedDate = y + '-' + mm + '-' + dd;

or use the following function, which is simple and you can use for date too

    function pad(d) {
return (d < 10) ? '0' + d.toString() : d.toString();
}
mm = pad(mm);

Get today's day and month in 2 digit format on click

According to this bug, FF strips leading zeroes from numeric input fields.

Setting an input's value to "003" would display "3" in the input
window, despite storing the value internally as "003".

Removing the field type will bypass this, though you should be cognizant of any effects this may cause when querying the field later. You may need parseInt.

Here's a fiddle that works on FF.

function leftPad(num, length) {
var result = '' + num;
while (result.length < length) {
result = '0' + result;
}
return result;
}

$("a.pick-today").on("click", function (event) {
var now = new Date();
var day = leftPad(now.getDate(), 2);
var month = leftPad(now.getMonth() + 1, 2);
var year = now.getFullYear();
var parent = $(this).parent('.input-holder');

parent.find('input.dd').val(day);
parent.find('input.mm').val(month);
parent.find('input.yyyy').val(year);
});

How do I get Month and Date of JavaScript in 2 digit format?

("0" + this.getDate()).slice(-2)

for the date, and similar:

("0" + (this.getMonth() + 1)).slice(-2)

for the month.

getMinutes() 0-9 - How to display two digit numbers?

var date = new Date("2012-01-18T16:03");

console.log( (date.getMinutes()<10?'0':'') + date.getMinutes() );

How to get 2 digit year w/ Javascript?

The specific answer to this question is found in this one line below:

//pull the last two digits of the year
//logs to console
//creates a new date object (has the current date and time by default)
//gets the full year from the date object (currently 2017)
//converts the variable to a string
//gets the substring backwards by 2 characters (last two characters)
console.log(new Date().getFullYear().toString().substr(-2));

Javascript add leading zeroes to date

Try this: http://jsfiddle.net/xA5B7/

var MyDate = new Date();
var MyDateString;

MyDate.setDate(MyDate.getDate() + 20);

MyDateString = ('0' + MyDate.getDate()).slice(-2) + '/'
+ ('0' + (MyDate.getMonth()+1)).slice(-2) + '/'
+ MyDate.getFullYear();

EDIT:

To explain, .slice(-2) gives us the last two characters of the string.

So no matter what, we can add "0" to the day or month, and just ask for the last two since those are always the two we want.

So if the MyDate.getMonth() returns 9, it will be:

("0" + "9") // Giving us "09"

so adding .slice(-2) on that gives us the last two characters which is:

("0" + "9").slice(-2)
"09"

But if MyDate.getMonth() returns 10, it will be:

("0" + "10") // Giving us "010"

so adding .slice(-2) gives us the last two characters, or:

("0" + "10").slice(-2)
"10"


Related Topics



Leave a reply



Submit