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( ...
随机推荐
- 内置函数系列之 filter
filter 过滤 基本语法: s = filter(function,iterable) 将可迭代对象的每一个元素,传进函数中,根据函数中的判断条件,返回True或False 返回True的是保留的 ...
- C语言真正的编译过程(4个步骤~~预编译,编译,汇编,连接)
转载自:https://www.cnblogs.com/wuyouxiaocai/p/5701088.html#commentform 说实话,很多人做了很久的C/C++,也用了很多IDE,但是对于可 ...
- C语言结构体篇 结构体
在描述一个物体的属性的时候,单一的变量类型是无法完全描述完全的.所以有了灵活的结构体变量. 结构体变量从意义上来讲是不同数据类型的组合,从内存上来讲是在一个空间内进行不同的大小划分. 1.1 结构体类 ...
- DFS:C 小Y的难题(1)
解题心得: 1.在明确使用DFS之后一定要找到递归函数的出口.方向,以及递归的点(在某个情况下开始递归)(void 也可以return,但是没有返回值).递归时也要有递归的方向,最后都能够达到递归的出 ...
- [Codeforces967C]Stairs and Elevators(二分查找)
[不稳定的传送门] Sloution 每次试一下最近的2个楼梯或者电梯就行了 Code #include <cstdio> #include <algorithm> #incl ...
- 初见akka-02:rpc框架
1.RPC:简单点说,就是多线程之间的通信,我们今天用了scala以及akka 来简单的实现了 rpc框架的一些简单的内容,一脸包括了,心跳,间隔时间, 注册以及一些问题, 模式匹配的一些东西,虽然比 ...
- android 事件传递机制
有三个方法: dispatchTouchEvent onInterceptTouchEvent onTouchEvent 首先:A的dispatchTouchEvent-A的onInterceptTo ...
- Maven学习 (二) Eclipse 上安装 Maven3插件
eclipse 安装插件的方式最常见的有两种: 1. 一种是在线安装,这貌似是用的最多的,就是:Help --> Install New Software,然后输入 HTTP 地址来安装,但 ...
- 容器技术的落地还要依靠SDN
容器能够实现新应用程序的快速部署,代表着目前IT开发社区的最热门趋势之一.然而,想要实现容器部署生产环境,IT人员还需要使用SDN技术,在分布式微应用程序之间实现可扩展.可管理且安全的通信. 什么是容 ...
- 《算法》C++代码 前言
现在大二正在上<数据结构>课,课内的书上代码实现很喜欢无脑用类.变量名字很长,而且常常实现太繁琐,并且代码有些无法运行,这些对于老手无所谓,但初学者看起来却会很不舒服.因此写点自己的代码, ...