js,将日期时分秒等格式化和转化
1.将js Date对象格式化为指定格式,添加一个原型方法
/**
* 返回指定format的string
* format eg:'yyyy-MM-dd hh:mm:ss'
**/
Date.prototype.format = function(format) {
var o = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S": this.getMilliseconds()
}
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
}
}
return format;
} 使用如:
new Date().format('yyyy-MM-dd'); // echo: '2015-12-01'
2.得到昨天 (永远相对于今天的前一天)
// 得到昨天的日期 返回昨天
function yesterday() {
var today = new Date();
var the_yesterday = today.setDate(today.getDate() - 1);
return the_yesterday;
}
3. 得到上一个月的今天
// 得到近一个月 返回上一个月的今天
function lastMonth() {
var today = new Date();
var month = today.getMonth();
var the_last_month;
if (month == 0) {
the_last_month = today.setMonth(11);
} else {
var the_last_month = today.setMonth(month - 1);
}
the_last_month = new Date(the_last_month).format('yyyy-MM-dd');
return the_last_month;
}
4.得到指定格式的最近一周 返回一个数组
// 得到近一周 返回数组 (note: 这里的format使用了上面的用来format原型方法)
function lastWeek(format) {
var today = new Date();
var the_week = [];
var format = format || 'yyyy-MM-dd';
for (var i = 1; i < 8; i++) {
var temp = today.setDate(today.getDate()-1);
temp = new Date(temp).format(format);
the_week.unshift( temp );
};
return the_week;
}
5.得到近n天 返回一个数组
// 得到近n天 返回数组
function lastNDay(days,format) {
var today = new Date();
var the_days = [];
var format = format || 'yyyy-MM-dd';
for (var i = 1; i < days+1; i++) {
var temp = today.setDate(today.getDate()-1);
temp = new Date(temp).format(format);
the_days.unshift( temp );
};
return the_days;
} //使用如
lastNDay(7); // 返回最近一周的日期数组 format参数可选 // [ '2011-11-01', '2011-11-02','2011-11-03','2011-11-04','2011-11-05','2011-11-06','2011-11-07' ]
6.把毫秒转换成时长
// 把毫秒转换成时长
// 返回 1.若小于等于60秒,显示秒数
// 2.若大于1分钟小于1小时,显示分钟
// 3.若大于1小时,显示x小时x分钟
function MillisecondToTime(msd) {
var time = parseInt(msd) / 1000;
if (time <= 60) {
time = time + '秒';
return time;
} else if (time > 60 && time < 60 * 60) {
time = parseInt(time / 60) + "分钟";
return time;
} else {
var hour = parseInt(time / 3600) + "小时";
var minute = parseInt(parseInt(time % 3600) / 60) + "分钟";
time = hour + minute;
return time;
}
}
js,将日期时分秒等格式化和转化的更多相关文章
- JS获取年月日时分秒
var d = new Date(); ) + "-" + d.getDate() + " " + d.getHours() + ":" + ...
- JS的日期的知识与格式化
需要知道的JS的日期的知识,都在这了 https://juejin.im/post/5d12b308f265da1b7c612746?utm_source=gold_browser_extension ...
- js实现将时分秒转化成毫秒,将秒转化成时分秒
// 时间转为毫秒 timeToSec(time) { var hour = time.split('[0] var min = time.split('[1] var sec = time.spli ...
- js动态实现时分秒
<div id="time" style="color: #96C2DD;</div> <script type="text/ ...
- jquery.datepair日期时分秒选择器
jquery.datepair是一个轻量级的jQuery插件,智能选择日期和时间范围,灵感来自于谷歌日历.Datepair将保持开始和结束日期/时间同步,并可以根据用户的操作设置默认值.该插件不提供任 ...
- js实现计时 时分秒
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- js获取年月日时分秒星期
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- js 倒计时 (时分秒版本)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- js获取当前时间的年月日时分秒以及时间的格式化
1.获取当前时间 var myDate = new Date(); 2.获取时间中的年月日时分秒 myDate.getYear(); // 获取当前年份(2位) myDate.getFullYear( ...
随机推荐
- [BZOJ1045] [HAOI2008] 糖果传递 (中位数)
Description 题目链接 Solution 这题跟数列的中位数有关, 具体证明见刘汝佳的蓝皮书里 Code #include <cstdio> #include <algor ...
- android的dmtracedump工具生成trace文件图片 'dot' 不是内部或外部命令,也不是可运行的程序 或批处理文件。
http://jingyan.baidu.com/article/c910274bfa6c1fcd361d2df7.html http://www.cnblogs.com/albert1017/p/3 ...
- JavaScript 仿ios滑动选择器
从git上找了一个,不过不是我想要的,更改了许多.到了我想要的效果: roller_selector_0_9.js 首先上js: "use strict"; /* * Author ...
- Webpack标准配置
let htmlWebpckPlugin= require('html-webpack-plugin');//该组件能将src下面提定的html文件与打包后在js文件打包在一起module.expor ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目9
2014-04-25 20:37 题目:请设计一个字节对齐的malloc函数,配套上对应的free函数.要求这个函数分配出的内存块儿的首地址是某个值n的整数倍,n是2的整次幂,比如128.1024之类 ...
- Object Pascal中文手册 经典教程
Object Pascal 参考手册 (Ver 0.1)ezdelphi@hotmail.com OverviewOverview(概述)Using object pascal(使用 object p ...
- Google浏览器历史版本下载地址和驱动器对应关系地址分享
Google浏览器历史版本下载地址https://www.slimjet.com/chrome/google-chrome-old-version.php google webdriver下载地址分享 ...
- 浅谈==和equals的区别
java中的==和equals的区别? 看上面的代码,输出的结果是: true false true true 1.为什么会有上面的区别呢?==和equals比较的到底是什么呢? 搞清楚两者的区别其实 ...
- 聊聊、Mybatis XML
引入相应的依赖包 <dependency><groupId>org.mybatis</groupId><artifactId>mybatis-sprin ...
- CPU指令集不同导致的core分析
最近程序需要支持CGSL系统运行,测试中发现相同操作系统的两台机器,编译机运行正常,测试机coredump.core信息汇总如下,可以看出是由于测试机不支持编译后的指令导致的问题: Program t ...