js时间戳转换日期格式和日期计算

简介: 一、时间戳转换日期 1 function formatDate(datetime) { 2 // 获取年月日时分秒值 slice(-2)过滤掉大于10日期前面的0 3 var year = datetime.

一、时间戳转换日期

 1 function formatDate(datetime) {
 2         // 获取年月日时分秒值  slice(-2)过滤掉大于10日期前面的0
 3         var year = datetime.getFullYear(),
 4             month = ("0" + (datetime.getMonth() + 1)).slice(-2),
 5             date = ("0" + datetime.getDate()).slice(-2),
 6             hour = ("0" + datetime.getHours()).slice(-2),
 7             minute = ("0" + datetime.getMinutes()).slice(-2),
 8             second = ("0" + datetime.getSeconds()).slice(-2);
 9         // 拼接
10         var result = year + "-"+ month +"-"+ date +" "+ hour +":"+ minute +":" + second;
11         // 返回
12         return result;
13     }
14 
15     var date = new Date();
16     console.log(formatDate(date)); // 2018-05-26 23:09:26

二、合同日期计算

根据开始日期和期限,计算结束日期

 1 //date: 日期字符串yyyy-MM-dd,如:2016-02-14
 2 //years:年份,正整数字符串
 3 //返回日期字符串yyyy-MM-dd,如:2016-02-14
 4     function dateAddYear(date, years) {
 5         var now = new Date(date);
 6         var intYear = now.getFullYear() + parseInt(years);
 7         var intMonth = now.getMonth() + 1; //正常的月份,
 8         var intDay = now.getDate() - 1; //日期-1
 9         if (intDay == 0) {
10             intMonth--; //减少一个月
11             if (intMonth == 0) {
12                 intYear--; //0:减少一年
13                 intMonth = 12;
14                 intDay = 31;
15             }
16             else if (intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) {
17                 intDay = 30; //4,6,9,11:30天
18             }
19             else if (intMonth == 2) {
20                 intDay = 28; //2:28/29
21                 if (intYear % 4 == 0) {
22                     intDay = 29;
23                 }
24             } else {
25                 intDay = 31; //1,3,5,7,8,10,12 :31天
26             }
27         }
28 
29         var strMonth = (intMonth) < 10 ? "0" + (intMonth).toString() : (intMonth).toString();
30         var strDay = (intDay) < 10 ? "0" + (intDay).toString() : (intDay).toString();
31         var strEndDate = intYear + "-" + strMonth + "-" + strDay;
32         return strEndDate;
33     }
34     
35     console.log(dateAddYear('2018-6-10','2')); //  2020-06-09

三、根据开始日期,计算count天过后的日期

beginDate是开始日期,字符串格式

count是指多少天,整型数

注意:setDate和getDate结合使用

date.setDate(date.getDate() + count);

 1 function calculateDate(beginDate,count){
 2         var date = new Date(beginDate);
 3         date.setDate(date.getDate() + count);
 4         var year = date.getFullYear();
 5         var month = ("0" + (date.getMonth()+1)).slice(-2);
 6         var day = ("0" + date.getDate()).slice(-2);
 7         var hours = ("0" + date.getHours()).slice(-2);
 8         var minute = ("0" + date.getMinutes()).slice(-2);
 9         var second = ("0" + date.getSeconds()).slice(-2);
10 
11         var endDate = year + "-"+ month +"-"+ day +" "+ hours +":"+ minute +":" + second;
12 
13         return endDate;
14     }
15 
16     console.log(calculateDate('2018-5-26 23:50:32',30)); // 2018-06-25 23:50:32

四、计算n月之后的日期

 1 function addMonth(date,monthNum){
 2    var str = date.split('-');
 3    var day = parseInt(str[2]);
 4    var nextMonth = new Date( str[0], parseInt(str[1])+monthNum, 0);
 5    var max = nextMonth.getDate();
 6    endDate = new Date( str[0],str[1]-1+monthNum,day>max? max: day );
 7    return endDate.toLocaleDateString().match(/\d+/g).join('-');
 8 }
 9 
10 console.log(addMonth("2018-12-12",12));

 

 1 function addMonth(date,monthNum){
 2 
 3    var str = date.split('-');
 4    var oldDate = new Date(str[0], parseInt(str[1]), 0);
 5    var oldDay = oldDate.getDate();
 6    console.log(oldDay);
 7    var day = parseInt(str[2]);
 8    var nextMonth = new Date( str[0], parseInt(str[1])+monthNum, 0);
 9    var max = nextMonth.getDate();
10    if(day >28 && day < 31){
11            max = max - (oldDay - day);
12            console.log(max);
13    }
14    endDate = new Date( str[0],str[1]-1+monthNum,day>max? max: day );
15    return endDate.toLocaleDateString().match(/\d+/g).join('-');
16 }
17 
18 console.log(addMonth("2018-1-27",1));

 

五、常用的Date对象方法

  1. Date()  返回当日的日期和时间。  
  2. getDate()   从 Date 对象返回一个月中的某一天 (1 ~ 31)。  
  3. getDay()    从 Date 对象返回一周中的某一天 (0 ~ 6)。  
  4. getMonth()  从 Date 对象返回月份 (0 ~ 11)。  
  5. getFullYear()   从 Date 对象以四位数字返回年份。  
  6. getYear()   请使用 getFullYear() 方法代替。  
  7. getHours()  返回 Date 对象的小时 (0 ~ 23)。  
  8. getMinutes()    返回 Date 对象的分钟 (0 ~ 59)。  
  9. getSeconds()    返回 Date 对象的秒数 (0 ~ 59)。  
  10. getMilliseconds()   返回 Date 对象的毫秒(0 ~ 999)。  
  11. getTime()   返回 1970 年 1 月 1 日至今的毫秒数。  
  12. getTimezoneOffset() 返回本地时间与格林威治标准时间 (GMT) 的分钟差。  
  13. getUTCDate()    根据世界时从 Date 对象返回月中的一天 (1 ~ 31)。  
  14. getUTCDay() 根据世界时从 Date 对象返回周中的一天 (0 ~ 6)。  
  15. getUTCMonth()   根据世界时从 Date 对象返回月份 (0 ~ 11)。  
  16. getUTCFullYear()    根据世界时从 Date 对象返回四位数的年份。  
  17. getUTCHours()   根据世界时返回 Date 对象的小时 (0 ~ 23)。  
  18. getUTCMinutes() 根据世界时返回 Date 对象的分钟 (0 ~ 59)。  
  19. getUTCSeconds() 根据世界时返回 Date 对象的秒钟 (0 ~ 59)。  
  20. getUTCMilliseconds()    根据世界时返回 Date 对象的毫秒(0 ~ 999)。  
  21. parse() 返回1970年1月1日午夜到指定日期(字符串)的毫秒数。  
  22. setDate()   设置 Date 对象中月的某一天 (1 ~ 31)。  
  23. setMonth()  设置 Date 对象中月份 (0 ~ 11)。  
  24. setFullYear()   设置 Date 对象中的年份(四位数字)。  
  25. setYear()   请使用 setFullYear() 方法代替。  
  26. setHours()  设置 Date 对象中的小时 (0 ~ 23)。  
  27. setMinutes()    设置 Date 对象中的分钟 (0 ~ 59)。  
  28. setSeconds()    设置 Date 对象中的秒钟 (0 ~ 59)。  
  29. setMilliseconds()   设置 Date 对象中的毫秒 (0 ~ 999)。  
  30. setTime()   以毫秒设置 Date 对象。  
  31. setUTCDate()    根据世界时设置 Date 对象中月份的一天 (1 ~ 31)。  
  32. setUTCMonth()   根据世界时设置 Date 对象中的月份 (0 ~ 11)。  
  33. setUTCFullYear()    根据世界时设置 Date 对象中的年份(四位数字)。  
  34. setUTCHours()   根据世界时设置 Date 对象中的小时 (0 ~ 23)。  
  35. setUTCMinutes() 根据世界时设置 Date 对象中的分钟 (0 ~ 59)。  
  36. setUTCSeconds() 根据世界时设置 Date 对象中的秒钟 (0 ~ 59)。  
  37. setUTCMilliseconds()    根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。  
  38. toSource()  返回该对象的源代码。  
  39. toString()  把 Date 对象转换为字符串。  
  40. toTimeString()  把 Date 对象的时间部分转换为字符串。  
  41. toDateString()  把 Date 对象的日期部分转换为字符串。  
  42. toGMTString()   请使用 toUTCString() 方法代替。  
  43. toUTCString()   根据世界时,把 Date 对象转换为字符串。  
  44. toLocaleString()    根据本地时间格式,把 Date 对象转换为字符串。  
  45. toLocaleTimeString()    根据本地时间格式,把 Date 对象的时间部分转换为字符串。  
  46. toLocaleDateString()    根据本地时间格式,把 Date 对象的日期部分转换为字符串。  
  47. UTC()   根据世界时返回 1970 年 1 月 1 日 到指定日期的毫秒数。  
  48. valueOf()   返回 Date 对象的原始值。 
相关文章
|
5月前
|
JavaScript 前端开发
JavaScript Date(日期) 对象
JavaScript Date(日期) 对象
72 2
|
4月前
|
JavaScript 前端开发
|
4月前
|
算法 开发者
Moment.js库是如何处理不同浏览器的时间戳格式差异的?
总的来说,Moment.js 通过一系列的技术手段和策略,有效地处理了不同浏览器的时间戳格式差异,为开发者提供了一个稳定、可靠且易于使用的时间处理工具。
150 57
|
4月前
|
JavaScript 前端开发 搜索推荐
Moment.js、Day.js、Miment,日期时间库怎么选?
【10月更文挑战第29天】如果你需要一个功能强大、插件丰富的日期时间库,并且对性能要求不是特别苛刻,Moment.js是一个不错的选择;如果你追求极致的轻量级和高性能,那么Day.js可能更适合你;而如果你有一些特定的日期时间处理需求,并且希望在性能和功能之间取得平衡,Miment也是可以考虑的。
174 57
|
4月前
|
JavaScript 前端开发
Moment.js与其他处理时间戳格式差异的JavaScript库相比有什么优势?
Moment.js与其他处理时间戳格式差异的JavaScript库相比有什么优势?
|
4月前
|
前端开发 JavaScript UED
"前端小技巧大揭秘:JS如何将后台时间戳秒变亲切小时前、分钟前,让用户秒懂,提升互动体验!"
【10月更文挑战第23天】在Web开发中,将后台返回的时间戳转换为“小时前”、“分钟前”、“刚刚”等友好的时间描述是常见需求。本文介绍如何用JavaScript实现这一功能,通过计算当前时间和时间戳的差值,返回相应的描述,提升用户体验。
77 1
|
5月前
|
缓存 JavaScript 前端开发
探索Vue.js中的计算属性与侦听器
【10月更文挑战第5天】探索Vue.js中的计算属性与侦听器
59 1
|
5月前
|
缓存 JavaScript 前端开发
深入理解Vue.js中的计算属性与侦听属性
【10月更文挑战第5天】深入理解Vue.js中的计算属性与侦听属性
62 0
|
5月前
|
缓存 JavaScript 前端开发
探索Vue.js中的计算属性与侦听器:深入理解与实践
【10月更文挑战第5天】探索Vue.js中的计算属性与侦听器:深入理解与实践
45 0
|
5月前
|
JavaScript 前端开发
在JavaScript中如何获取时间戳?
在JavaScript中如何获取时间戳?
158 0

热门文章

最新文章