博客
关于我
js 比较日期大小(js获取当前日期,转化后比较日期大小)
阅读量:608 次
发布时间:2019-03-12

本文共 1408 字,大约阅读时间需要 4 分钟。

当前时间是否在9月1日--11月15日之间(每年)

技术实现代码解释

时间判断函数

function compareTime() {    const date = new Date();    const nowYear = date.getFullYear();    const day = new Date(        Date.parse(date.toLocaleDateString().replace(/-/g, "/"))    );        const s = nowYear + "/" + "09" + "/" + "01";    const e = nowYear + "/" + "11" + "/" + "15";    const start = new Date(Date.parse(s.replace(/-/g, "/")));    const end = new Date(Date.parse(e.replace(/-/g, "/")));        return !(day < start || day > end);}

时间戳转换说明

Date.parse() 方法返回从1970年1月1日0点到指定日期的时间戳,单位为毫秒。

显示格式示例:

  • new Date().getFullYear():获取完整年份(4位)
  • new Date().getMonth() + 1:月份(0-11,0代表1月)
  • new Date().getDate():日期(1-31)

现有实现方法

// 获取当前年份(4位)const nowYear = new Date().getFullYear();// 获取当前日期对象const day = new Date(    Date.parse(new Date().toLocaleDateString().replace(/-/g, "/")));

代码执行效果

const s = `${nowYear}/09/01`;const e = `${nowYear}/11/15`;const start = new Date(Date.parse(s));const end = new Date(Date.parse(e));

时间范围判断逻辑

function compareTime() {    const day = new Date(        Date.parse(new Date().toLocaleDateString().replace(/-/g, "/"))    );        const s = `${new Date().getFullYear()}/09/01`;    const e = `${new Date().getFullYear()}/11/15`;    const start = new Date(Date.parse(s));    const end = new Date(Date.parse(e));        return !(day < start || day > end);}

时间验证方法

const day = new Date();if (day < start || day > end) {    return false;}

转载地址:http://gzgxz.baihongyu.com/

你可能感兴趣的文章
MYSQL 主从同步文档的大坑
查看>>
mysql 主键重复则覆盖_数据库主键不能重复
查看>>
Mysql 事务知识点与优化建议
查看>>
Mysql 优化 or
查看>>
mysql 优化器 key_mysql – 选择*和查询优化器
查看>>
MySQL 优化:Explain 执行计划详解
查看>>
Mysql 会导致锁表的语法
查看>>
mysql 使用sql文件恢复数据库
查看>>
mysql 修改默认字符集为utf8
查看>>
Mysql 共享锁
查看>>
MySQL 内核深度优化
查看>>
mysql 内连接、自然连接、外连接的区别
查看>>
mysql 写入慢优化
查看>>
mysql 分组统计SQL语句
查看>>
Mysql 分页
查看>>
Mysql 分页语句 Limit原理
查看>>
MySql 创建函数 Error Code : 1418
查看>>
MySQL 创建新用户及授予权限的完整流程
查看>>
mysql 创建表,不能包含关键字values 以及 表id自增问题
查看>>
mysql 删除日志文件详解
查看>>