Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date #31

Open
yym-yumeng123 opened this issue Sep 9, 2017 · 0 comments
Open

Date #31

yym-yumeng123 opened this issue Sep 9, 2017 · 0 comments

Comments

@yym-yumeng123
Copy link
Owner

创建日期

  • 表示当前时间
new Date()
  • 表示2017年9月1日
new Date(2017,8)  //月份从零开始,小一月
new Date(2017,4)  //2017-5

date.getXXX()

  • 获取时间
var date = new Date(2017,9,3,14,57,18)  //2017-10-3 14:57:18
date.getFullYear()  //2017
date.getMouth()  //9
date.getDate()  //3
date.getHours()  //14
date.getMinutes  //57
date.getSeconds()  //18

var date = new Date();
alert('今天是' + date.getFullYear() + '年' + (date.getMonth() + 1) + '月' + date.getDate() + '日');
alert('现在是北京时间' + date.getHours() + '点' + date.getMinutes() + '分' + date.getSeconds() + '秒');

我们希望得到的时间是看的简单的,这样的(2017-10-3 14:57:18)但是在浏览器上的到的结果是这样的

var date = new Date(2017,9,3,14,57,18)
date
Tue Oct 03 2017 14:57:18 GMT+0800 (中国标准时间)

格式化

function padding(number){
  return number < 10 ? '0' + number : '' + number;
}
function format(date) {
  return date.getFullYear() + '-' 
  + padding(date.getMonth() + 1) + '-' 
  + padding(date.getHours()) + ':' 
  + padding(date.getMinutes()) + ':' 
  + padding(date.getSeconds());
}
var date = new Date();  
alert(date);  //Fri Sep 01 2017 11:24:40 GMT+0800 (中国标准时间)
alert(format(date));  //2017-09-01 11:24:40

date.setXXX()

  • 设置日期
var date = new Date(2017,9,3,14,57,18)  //2017-10-3 14:57:18
date.setFullYear(2050)  //2050-10-3 14:57:18
date.setMouth(11)  //2050-12-3 14:57:18
date.setDate(16)  ///2050-12-16 14:57:18

如果是

date.setDate(35)  //2017-11-4,自动计算

求天数?

function getDays(year, month){
	var date = new Date(year, month, 0);
	return date.getDate();
}

alert('2001年2月有' + getDays(2001, 2) + '天。');
alert('2001年3月有' + getDays(2001, 3) + '天。');

Date -> Number

var date = new Date(2017,9,3,14,57,18)
date.getTime()  //1507013838000 距1970-1-1 00:00:00的毫秒数

Number -> Date

new Date(1507013838000)
date.getTime(1507013838000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant