【JS 获取日期时间方法与常用的组合方法】

JS提供的方法

1. Date()

Date() 是 JavaScript 中内置的一个对象,可以获取当前的日期和时间。Date() 对象的 toString() 方法可以将其转换为字符串形式。

例如:

var today = new Date();
console.log(today.toString()); // 输出当前的日期和时间

2. getTime()

getTime() 方法返回的是从 1970 年 1 月 1 日 00:00:00 UTC 到当前时间的毫秒数。

例如:

var today = new Date();
console.log(today.getTime());

3. getFullYear()

getFullYear() 方法返回当前年份(4 位数字格式)。

例如:

var today = new Date();
console.log(today.getFullYear());

4. getMonth()

getMonth() 方法返回当前月份的数字(0 到 11,0 表示一月,1 表示二月,依此类推)。

例如:

var today = new Date();
console.log(today.getMonth());

5. getDate()

getDate() 方法返回当前月份的日期(1 到 31)。

例如:

var today = new Date();
console.log(today.getDate());

6. getDay()

getDay() 方法返回当前星期的数字(0 到 6,0 表示星期日,1 表示星期一,依此类推)。

例如:

var today = new Date();
console.log(today.getDay());

7. getHours()

getHours() 方法返回当前时间的小时数(0 到 23)。

例如:

var today = new Date();
console.log(today.getHours());

8. getMinutes()

getMinutes() 方法返回当前时间的分钟数(0 到 59)。

例如:

var today = new Date();
console.log(today.getMinutes());

9. getSeconds()

getSeconds() 方法返回当前时间的秒数(0 到 59)。

例如:

var today = new Date();
console.log(today.getSeconds());

10. getMilliseconds()

getMilliseconds() 方法返回当前时间的毫秒数(0 到 999)。

例如:

var today = new Date();
console.log(today.getMilliseconds());

常用组合方法

获取年月日

getDate: function(type) {
		const date = new Date();
		
		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		let day = date.getDate();

		month = month > 9 ? month : '0' + month;;
		day = day > 9 ? day : '0' + day;
		//判断链接方式
		if (type == '.') {
				return `${year}.${month}.${day}`;
			} else {
				return `${year}-${month}-${day}`;
			}
	}

获取时分秒

getTime: function(){
		const now = new Date();
		const hour = now.getHours();
		const minute = now.getMinutes();
		const second = now.getSeconds();
		return `${hour}:${minute}:${second}`;
}

获取距离现在的时间(几分钟前,昨天…)

timestampFormat: function(timestamp) {
		if (isNaN(timestamp)) {
			let _date = new Date(timestamp.replace(/-/g, '/'));
			timestamp = _date.getTime() / 1000;
		}

		function zeroize(num) {
			return (String(num).length == 1 ? '0' : '') + num;
		}

		let curTimestamp = parseInt(new Date().getTime() / 1000); //当前时间戳
		let timestampDiff = curTimestamp - timestamp; // 参数时间戳与当前时间戳相差秒数

		let curDate = new Date(curTimestamp * 1000); // 当前时间日期对象
		let tmDate = new Date(timestamp * 1000); // 参数时间戳转换成的日期对象

		let Y = tmDate.getFullYear(),
			m = tmDate.getMonth() + 1,
			d = tmDate.getDate();
		let H = tmDate.getHours(),
			i = tmDate.getMinutes(),
			s = tmDate.getSeconds();

		if (timestampDiff < 60) { // 一分钟以内
			return "刚刚";
		} else if (timestampDiff < 3600) { // 一小时前之内
			return Math.floor(timestampDiff / 60) + "分钟前";
		} else if (curDate.getFullYear() == Y && curDate.getMonth() + 1 == m && curDate.getDate() == d) {
			return '今天' + zeroize(H) + ':' + zeroize(i);
		} else {
			let newDate = new Date((curTimestamp - 86400) * 1000); // 参数中的时间戳加一天转换成的日期对象
			if (newDate.getFullYear() == Y && newDate.getMonth() + 1 == m && newDate.getDate() == d) {
				return '昨天' + zeroize(H) + ':' + zeroize(i);
			} else if (curDate.getFullYear() == Y) {
				return zeroize(m) + '-' + zeroize(d) + '-' + zeroize(H) + ':' + zeroize(i);
			} else {
				return Y + '-' + zeroize(m) + '-' + zeroize(d) + ' ' + zeroize(H) + ':' + zeroize(i);
			}
		}
	}

获取本周,上周,上月 时间段

// 获取本周日期范围
const today = new Date();
const dayOfWeek = today.getDay() || 7; // 获取本周第几天,默认第一天是周一,所以如果是周日需要加1
const firstDayOfWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1 - dayOfWeek);
const lastDayOfWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 7 - dayOfWeek);

// 获取上周日期范围
const firstDayOfLastWeek = new Date(firstDayOfWeek.getFullYear(), firstDayOfWeek.getMonth(), firstDayOfWeek.getDate() - 7);
const lastDayOfLastWeek = new Date(lastDayOfWeek.getFullYear(), lastDayOfWeek.getMonth(), lastDayOfWeek.getDate() - 7);

// 获取上个月日期范围
const year = today.getFullYear();
const month = today.getMonth() - 1;
const firstDayOfLastMonth = new Date(year, month, 1);
const lastDayOfLastMonth = new Date(year, month + 1, 0);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值