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

一、时间戳转换日期

 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

四、常用的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 对象的原始值。

原文地址:https://www.cnblogs.com/le220/p/9094838.html

时间: 2024-08-28 10:52:49

js时间戳转换日期格式和日期计算的相关文章

js 时间戳转换成格式化日期 日期格式化

timestamp缺省表示使用当前时间戳,formats默认格式是Y-m-d,例如2018-01-01. 完整代码: 1 /* 2 ** 时间戳转换成指定格式日期 3 ** eg. 4 ** dateFormat(11111111111111, 'Y年m月d日 H时i分') 5 ** → "2322年02月06日 03时45分" 6 */ 7 var dateFormat = function (timestamp, formats) { 8 // formats格式包括 9 // 1

js时间戳和时间格式之间的转换

js里面的data对象的运行 很实用的东西,, 直接上代码: //时间戳转换成日期时间2014-8-8 下午11:40:20 function formatDate(ns){ return new Date(parseInt(ns) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " "); } //时间戳转换成八位日期2014-5-5 function userDate(uData){ var

java转换ddMMMyyyy 格式的日期(12MAY2018)月三个字母

SimpleDateFormat oldFormat = new SimpleDateFormat("ddMMMyyyy", Locale.ENGLISH); Date parseDate = oldFormat.parse(str); java API: SimpleDateFormat public SimpleDateFormat(String pattern,Locale locale) 用给定的模式和给定语言环境的默认日期格式符号构造 SimpleDateFormat.注:此

AngularJS过滤器filter-时间日期格式-渲染日期格式-$filter

今天遇到了这些问题索性就 写篇文章吧 话不多说直接上栗子 不管任何是HTML格式还是JS格式必须要在  controller 里面写 // new Date() 获取当前时间 yyyy-MM-ddd //是返回的格式 下面会一 一列出 这种返回格式$scope.wwwwwww = $filter(new Date(),'yyyy-MM-dd') //或者这样写//这样是 指定date类型 可以省略不写 下面的是多此一举 但是我感觉有人会钻牛角尖所以嘛 嘿嘿 $scope.wwwwwww = $f

jquery 前台页面中时间戳转换成正常的日期格式

这个方法网上有好多,不过值得注意的是,你后台传过去的时间戳是不是精确到毫秒的问题 如果精确到毫秒就用下面的这个形式 new Date(parseInt(iteam.time)).toLocaleString().replace(/:\d{1,2}$/,' ') 如果没有精确到毫秒就用这种形式 new Date(parseInt(iteam.time)*1000).toLocaleString().replace(/:\d{1,2}$/,' ') 输出的结果是

js日期格式,日期对象

以对象为基准去使用方法, 围绕Date对象 var a = new Date() 返回当前的时间对象,可以使用内置的日期对象的方法 a.getFullYear(), a.getMonth(), a.getDate() 从当前时间得到 年.月.日 手动设置某一个时间, new Date(a.getFullYear(), a.getMonth(), a.getDate(), 0, 0, 0, 0); 后面四个是 时.分.秒.毫秒设置一个指定日期 var b = new Date().setDate(

js 时间戳转换成时间格式,可自定义格式

由于 c# 通过ajax获取的时间 传到前台 格式为:/Date(1354116249000)/ 所以需要转换一下,想要什么格式 更改 format() 里的 返回语句 就可以了 formatDate()方法传入的参数是时间戳,可以用replace()得到时间戳:replace("/Date(", "").replace(")/", ""),然后传入方法,就可以得到时间格式了 function formatDate(obj)

JS如何将CST格式的日期转换为制定格式String

<html> <body> <script type="text/javascript"> var d = new Date() dateFormat = function (date, format) { date = new Date(date); var o = { 'M+' : date.getMonth() + 1, //month 'd+' : date.getDate(), //day 'H+' : date.getHours(), /

JS时间戳转时间格式

//转化为时间格式 function getDate(timestamp) { timestamp = timestamp.replace("/Date(", "").replace(")/", ""); if (timestamp.indexOf("+") > 0) { timestamp = timestamp.substring(0, timestamp.indexOf("+"