众所周知,JavaScript核心包含Data()构造函数,用来创建表示时间和日期的对象。

今天主要跟大家梳理一下,常用的时间、日期处理方法,方便大家使用和理解

格式化时间

老生常谈,大概会这么写

1
2
3
4
5
6
7
8
9
10
11
var format = function (time) { 
var y = time.getFullYear(); //getFullYear方法以四位数字返回年份
var M = time.getMonth() + 1; // getMonth方法从 Date 对象返回月份 (0 ~ 11),返回结果需要手动加一
var d = time.getDate(); // getDate方法从 Date 对象返回一个月中的某一天 (1 ~ 31)
var h = time.getHours(); // getHours方法返回 Date 对象的小时 (0 ~ 23)
var m = time.getMinutes(); // getMinutes方法返回 Date 对象的分钟 (0 ~ 59)
var s = time.getSeconds(); // getSeconds方法返回 Date 对象的秒数 (0 ~ 59)
return y + '-' + M + '-' + d + ' ' + h + ':' + m + ':' + s;
} var time1 = format(new Date());

但是有什么问题呢?一般来说小于10的值,要在前面添加字符串‘0’的,我们大可以写个判断来解决他,但是太麻烦了~

其实可以这样

1
2
3
4
5
6
7
8
var format = function (time) { 
var date = new Date(+time + 8 * 3600 * 1000);
return date.toJSON().substr(0, 19).replace('T', ' ').replace(/-/g, '.');
}
var time1 = format(new Date()); //Date的‘toJSON’方法返回格林威治时间的JSON格式字符串,转化为北京时间需要额外增加8个时区,然后把‘T’替换为空格,即是我们需要的时间格式,后面可以通过正则将日期分隔符换成任何想要的字符。
//一元加操作符可以把任何数据类型转换成数字,所以获取时间对象对应毫秒数的另一个方法是+Date或Number(Date)

获取当月最后一天

一个月可能有28/29/30/31天,使用写死数据的方式来解决闰年和大小月显然是不科学的。

1
2
3
4
5
6
7
8
function getLastDayOfMonth (time) {
var month = time.getMonth();
time.setMonth(month+1);
time.setDate(0);
return time.getDate()
}
getLastDayOfMonth(new Date())
//先月份加一,再取上个月的最后一天

获取这个季度第一天

用来确定当前季度的开始时间,常用在报表中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function getFirstDayOfSeason (time) {
var month = time.getMonth();
if(month <3 ){
time.setMonth(0);
}else if(2 < month && month < 6){
time.setMonth(3);
}else if(5 < month && month < 9){
time.setMonth(6);
}else if(8 < month && month < 11){
date.setMonth(9);
}
time.setDate(1);
return time;
}
getFirstDayOfSeason(new Date())
//先月份加一,再取上个月的最后一天

获取中文星期

这也是个比较常见的雪球,完全没必要写一长串switch啦,直接用charAt来解决。

1
let time ="日一二三四五六".charAt(new Date().getDay());

获取今天是当年的第几天

来看看今年自己已经浪费了多少时光~

1
2
3
4
var time1 = Math.ceil(( new Date() - new Date(new Date().getFullYear().toString()))/(24*60*60*1000));

//需要注意的是new Date()不设置具体时间的话new Date(2019)得到的不是0点而是8点 
//Tue Jan 01 2019 08:00:00 GMT+0800 (中国标准时间)

获取今天是当年的第几周

日历、表单常用

1
2
3
var week = Math.ceil(((new Date() - new Date(new Date().getFullYear().toString()))/(24*60*60*1000))/7);

//在获取第几天的基础上除7,向上取整

获取今天是当年还剩多少天

再来看看今年还有多少天可以浪费~

1
2
3
4
5
6
7
8
9
function restOfYear(time) {
var nextyear = (time.getFullYear() + 1).toString();
var lastday = new Date(new Date(nextyear)-1); //获取本年的最后一毫秒:
console.log(lastday)
var diff = lastday - time; //毫秒数
return Math.floor(diff / (1000 * 60 * 60 * 24));
}
restOfYear(new Data())
//先取下一年第一秒,再减1毫秒。顺便思考一下为什么

最新文章

  1. python之面向对象
  2. auto_ptr源码剖析
  3. ContentProvider实现流程
  4. Bellman算法
  5. html checkbox 全选与反选
  6. [转].net中的认证(authentication)与授权(authorization)
  7. Network boot from AMD Am79C970A
  8. Java系列--第三篇 基于Maven的Android开发CAIO
  9. PHP学习笔记十六【方法】
  10. react-native 布局基础
  11. request.getParameter()与request.setAttribute()的区别 (转载)
  12. 彻底了解构建 JSON 字符串的三种方式
  13. SPOJ 1812 Longest Common Substring II
  14. 【nginx】解决Nginx重启时提示nginx: [emerg] bind() to 0.0.0.0:80错误
  15. 学习笔记12之通过ajax动态添加选项
  16. Python之路 day1 基础1 变量 for while 用户输入
  17. thinkphp3.2 实现二级导航和高亮显示
  18. 最新版本Firefox表单css兼容性
  19. Core WebAPI 入门
  20. oracle 11g 从 dmp 文件中导出 sql 代码 的方法.

热门文章

  1. Unity2D游戏开发之保卫萝卜
  2. gcc 编译两个so其中soA依赖soB
  3. gcc编译链接std::__cxx11::string和std::string的问题
  4. WebGL学习笔记(十二):加载模型文件
  5. 搭建npm私服流程
  6. 【Linux基础】vim如何显示文件名称
  7. spring boot使用WebClient调用其他系统提供的HTTP服务
  8. Ubuntu16.04 安装PHP7 的 imagick 扩展
  9. 浅谈MVC、MVVM的区别
  10. 在Grafana使用普罗米修斯