javascript tutorial - [Solved-5 Solutions] Documentation on formatting a date in JavaScript - javascript - java script - javascript array



Problem:

Where can we find documentation on formatting a date in JavaScript?

Solution 1:

I noticed that JavaScript's new Date() function is very smart in accepting dates in several formats.

Xmas95 = new Date("25 Dec, 1995 23:15:00")
Xmas95 = new Date("2009 06 12,12:52:39")
Xmas95 = new Date("20 09 2006,12:52:39")
click below button to copy the code. By JavaScript tutorial team

Solution 2:

<script type="text/javascript">
    var d = new Date();
    var curr_date = d.getDate();
    var curr_month = d.getMonth() + 1; //Months are zero based
    var curr_year = d.getFullYear();
    console.log(curr_date + "-" + curr_month + "-" + curr_year);
</script>

click below button to copy the code. By JavaScript tutorial team

Solution 3:

var a = moment([2010, 1, 14, 15, 25, 50, 125]);
a.format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Sunday, February 14th 2010, 3:25:50 pm"
a.format("ddd, hA"); 

click below button to copy the code. By JavaScript tutorial team

Solution 4:

function dateToYMD(date) {
    var d = date.getDate();
    var m = date.getMonth() + 1;
    var y = date.getFullYear();
    return '' + y + '-' + (m<=9 ? '0' + m : m) + '-' + (d <= 9 ? '0' + d : d);
}

click below button to copy the code. By JavaScript tutorial team

Solution 5:

When a single argument is passed to new Date(), it casts this function prototype:

new Date(value)
click below button to copy the code. By JavaScript tutorial team

When two or more arguments are passed to new Date(), it casts this function prototype:

new Date (year, month [, date [, hours [, minutes [, seconds [, ms ] ] ] ] ] )
click below button to copy the code. By JavaScript tutorial team

Related Searches to javascript tutorial - Documentation on formatting a date in JavaScript