UID684203性别保密经验 EP铁粒 粒回帖0主题精华在线时间 小时注册时间2022-10-30最后登录1970-1-1
| 本帖最后由 一只晨月鸭 于 2024-6-11 20:01 编辑
创建时间对象
- var currentDate = new Date();
- var specificDate = new Date(2024, 4, 28, 8, 50, 0, 0);
复制代码 获取年份
- var currentDate = new Date();
- var year = currentDate.getFullYear();
- console.log(year);
复制代码 获取月份
- var currentDate = new Date();
- var month = currentDate.getMonth() + 1;
- console.log(month);
复制代码 获取日期
- var currentDate = new Date();
- var date = currentDate.getDate();
- console.log(date);
复制代码 获取星期
- var currentDate = new Date();
- var dayOfWeek = currentDate.getDay();
- var daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
- var dayName = daysOfWeek[dayOfWeek];
- console.log(dayName);
复制代码 获取小时
- var currentDate = new Date();
- var hours = currentDate.getHours();
- console.log(hours);
复制代码 获取秒数
- var currentDate = new Date();
- var seconds = currentDate.getSeconds();
- console.log(seconds);
复制代码 获取当前时间戳
- var currentDate = new Date();
- var timestamp = currentDate.getTime();
- console.log(timestamp);
复制代码 转换当前时间戳
- var currentDate = new Date();
- var timestamp = currentDate.getTime();
- console.log(timestamp);
复制代码 指定日期转时间戳
- var specifiedDate = new Date(2024, 4, 28);
- var timestamp = specifiedDate.getTime();
- console.log(timestamp);
复制代码 时间戳转日期
- var timestamp = 1622195912345;
- var date = new Date();
- date.setTime(timestamp);
- var year = date.getFullYear();
- var month = date.getMonth() + 1;
- var day = date.getDate();
- console.log(year + '-' + month + '-' + day);
复制代码 计算两个世界的间隔
- var startDate = new Date('2024-05-01');
- var endDate = new Date('2024-05-10');
- var startTimestamp = startDate.getTime();
- var endTimestamp = endDate.getTime();
- var millisecondsPerDay = 24 * 60 * 60 * 1000;
- var daysInterval = Math.round((endTimestamp - startTimestamp) / millisecondsPerDay);
- console.log(daysInterval);
复制代码 判断时间是否正确
- function isValidDate(dateString) {
- var regex = /^\d{4}-\d{2}-\d{2}$/;
- if (!regex.test(dateString)) {
- return false;
- }
- var date = new Date(dateString);
- var year = date.getFullYear();
- var month = date.getMonth() + 1;
- var day = date.getDate();
- if (month < 1 || month > 12) {
- return false;
- }
- var daysInMonth = new Date(year, month, 0).getDate();
- if (day < 1 || day > daysInMonth) {
- return false;
- }
- return true;
- }
- var dateString = '2024-02-30';
- console.log(isValidDate(dateString));
复制代码 确定日期再哪一年的第几天
- function dayOfYear(dateString) {
- var date = new Date(dateString);
- var startOfYear = new Date(date.getFullYear(), 0, 0);
- var diff = date - startOfYear;
- var oneDay = 1000 * 60 * 60 * 24;
- var day = Math.floor(diff / oneDay);
- return day;
- }
- // 示例用法
- var dateString = '2024-05-28';
- var day = dayOfYear(dateString);
- console.log(day);
复制代码 格式化时间
- function formatTime(date) {
- var hours = date.getHours().toString().padStart(2, '0');
- var minutes = date.getMinutes().toString().padStart(2, '0');
- var seconds = date.getSeconds().toString().padStart(2, '0');
- return hours + ':' + minutes + ':' + seconds;
- }
- var now = new Date();
- var formattedTime = formatTime(now);
- console.log(formattedTime);
复制代码 |
评分查看全部评分
|