林梦泽 发表于 2024-6-11 19:45:37

JavaScript代码中常用的时间对象

本帖最后由 一只晨月鸭 于 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;
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);

镜梦 发表于 2024-6-11 21:35:05

我怎么在mengze2.cn上看过
页: [1]
查看完整版本: JavaScript代码中常用的时间对象