当前位置:   article > 正文

js获取当前日期(年份,月份,时间)_js获取当前年份

js获取当前年份

js获取当前日期(年份,月份,时间),实现方法:

function getDateTime (type) {
	var date = new Date();
	var hengGang = "-";
	var maoHao = ":";
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var curDate = date.getDate();
	var curHours = date.getHours();
	var curMinutes = date.getMinutes();
	var curSeconds = date.getSeconds();
	
	if (month >= 1 && month <= 9) {
	  month = "0" + month;
	}
	if (curDate >= 0 && curDate <= 9) {
	  curDate = "0" + curDate;
	}
	if (curHours >= 0 && curHours <= 9) {
	  curHours = "0" + curHours;
	}
	if (curMinutes >= 0 && curMinutes <= 9) {
	  curMinutes = "0" + curMinutes;
	}
	if (curSeconds >= 0 && curSeconds <= 9) {
	  curSeconds = "0" + curSeconds;
	}
	var currentdate = "";
	if (type == "year") {
	  currentdate = year;
	  return currentdate;
	} else if (type == "month") {
	  currentdate = year + hengGang + month;
	  return currentdate;
	} else {
	  currentdate = year + hengGang + month + hengGang + curDate + " " + curHours + maoHao + curMinutes + maoHao + curSeconds;
	  return currentdate;
	}
}
var year = getDateTime('year');  
console.log(year);  // 2021
var month = getDateTime('month');  
console.log(month); // 2021-12
var date = getDateTime('');
console.log(date);  // 2021-12-03 09:00:00
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/292335
推荐阅读
相关标签
  

闽ICP备14008679号