Good web front-end programmers to share objects Date

Good programmers web front-end share Date object, what is a Date object built-in objects Date:

The type of use from UTC (Coordinated Universal Time, Coordinated Universal Time) the number of milliseconds January 1970 midnight on the 1st (zero) elapsed to save the date. Save the Date type date accurate to 285616 years before January 1, 1970 or after.


Date Object how to use it?

First you have to get a Date object

 

To obtain micro - channel;

var d=new Date( );

 

When the object creation date, do not pass any parameters returned by default the current time;

 

var d=new Date( '2015/12/2');

 

 

In the case of the incoming parameters, obtained is passed in time;

 

Note: This parameter is a string.

 

Some methods:

 

1.d.getFullYear () Gets the current year. || d.setFullYear (2012) returns January 1, 1970 to set the number of milliseconds;

 

2.d.getMonth () Gets the current month (Note: a small BUG, ​​the current month starting from 0) || d.setMonth (9) returns the number of milliseconds set the month of January 1, 1970 to the current year ;

 

3.d.getDate () Gets the current date || d.setDate () above;

 

4. getHours () acquires the

    getMinutes () Gets minute

    the getSeconds () Gets seconds

 

Different acquisition time of each machine, because the method is the time to return the machine; not time international standards;

 

The modification date;

Date.parse ( "2015-08-24"); 1970 milliseconds to acquire the set time;

 

d.getTime (); 1970 milliseconds to acquire the current time;

 

d.setTime()

    new Date(time)

    Creating a date object, and specify the time you can specify the number of milliseconds

Or modify time attribute, var d = new Date (); d.setTime (56521211021); 

 

Case:

 

1. The date format

<!doctype html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Document</title>

</head>

<body>

</body>

<script>

function geshihua () {

var d = new Date();

var year = d.getFullYear();

var Month = d.getMonth() + 1;

var day = d.getDate();

var str = 'current time is:' + year + 'of' + Month + 'dated' + day + 'Day'

document.write(str);

}

geshihua ()

</script>

</html>

  1. Gets the number of days of a month:

<!doctype html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Document</title>

<script>

function days(year, month) {

var str = year + '/' + month + '/ 1';

var d = new Date(str);

var Month = d.getMonth();

var MonthMin = d.setMonth(Month);

var MonthMin2 = d.setMonth(Month + 1);

was MONTHDAY = MonthMin2 - MonthMin

alert(MonthDay / 24 / 60 / 60 / 1000)

}

days('2014', '2')

</script>

</head>

<body>

</body>

</html>

  1. Calculate the date difference

<!doctype html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Document</title>

<style>

.btn {

background:none;

border: 1px solid #b6b6b6;

display: block;

height: 40px;

margin: 40px auto;

}

</style>

</head>

<body>

<div id="div1">

<Input type = "text" placeholder = 'Start Year'>

<Input type = "text" placeholder = 'start month'>

<Input type = "text" placeholder = 'Start Date'> ||

<Input type = "text" placeholder = 'termination year'>

<Input type = "text" placeholder = 'termination month'>

<Input type = "text" placeholder = 'termination date'>

</div>

<Input type = "button" value = 'date calculated gap' class = "btn" onclick = 'jisuanriqi ()'>

</body>

<script>

function jisuanriqi () {

attire var = document.getElementById ( 'div1');

var aInput = oDiv.getElementsByTagName('input');

was qishiArr = [];

var zhongzhiArr = [];

for (var i = 0; i < aInput.length; i++) {

if (i < 3) {

qishiArr[i] = aInput[i].value;

} else {

zhongzhiArr[i] = aInput[i].value;

}

}

var str1 = qishiArr.join('/');

var str2 = zhongzhiArr.join('/');

var d1 = new Date(str1);

var d2 = new Date(str2);

alert(d1 + ":" + d2)

var days = Math.abs(d1.getTime() - d2.getTime()) / 1000 / 24 / 60 / 60;

alert(days)

}

</script>

</html>

Guess you like

Origin www.cnblogs.com/gcghcxy/p/11059488.html