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,将日期时分秒等格式化和转化的更多相关文章

  1. JS获取年月日时分秒

    var d = new Date(); ) + "-" + d.getDate() + " " + d.getHours() + ":" + ...

  2. JS的日期的知识与格式化

    需要知道的JS的日期的知识,都在这了 https://juejin.im/post/5d12b308f265da1b7c612746?utm_source=gold_browser_extension ...

  3. js实现将时分秒转化成毫秒,将秒转化成时分秒

    // 时间转为毫秒 timeToSec(time) { var hour = time.split('[0] var min = time.split('[1] var sec = time.spli ...

  4. js动态实现时分秒

    <div id="time" style="color: #96C2DD;</div>      <script type="text/ ...

  5. jquery.datepair日期时分秒选择器

    jquery.datepair是一个轻量级的jQuery插件,智能选择日期和时间范围,灵感来自于谷歌日历.Datepair将保持开始和结束日期/时间同步,并可以根据用户的操作设置默认值.该插件不提供任 ...

  6. js实现计时 时分秒

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. js获取年月日时分秒星期

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. js 倒计时 (时分秒版本)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. js获取当前时间的年月日时分秒以及时间的格式化

    1.获取当前时间 var myDate = new Date(); 2.获取时间中的年月日时分秒 myDate.getYear(); // 获取当前年份(2位) myDate.getFullYear( ...

随机推荐

  1. 记 判断手机号运营商function

    /* 移动:134.135.136.137.138.139.150.151.157(TD).158.159.187.188 联通:130.131.132.152.155.156.185.186 电信: ...

  2. 【js】【读书笔记】廖雪峰的js教程读书笔记

    最近在看廖雪峰的js教程,重温了下js基础,记下一些笔记,好记性不如烂笔头嘛 编写代码尽量使用严格模式 use strict JavaScript引擎是一个事件驱动的执行引擎,代码总是以单线程执行 执 ...

  3. 【转载】CentOS7.0下安装Telnet

    1..先检查CentOS7.0是否已经安装以下两个安装包:telnet-server.xinetd.命令如下: # rpm -qa telnet-server # rpm -qa xinetd 如果没 ...

  4. Small Talk Matters【闲谈很重要】

    Small Talk Matters We' ve all been there: in a lift, in line at the bank or on an airplane, 我们都有过这样的 ...

  5. Social Media Addiction【社交媒体上瘾】

    Social Media Addiction Children as young as ten are becoming dependent on social media for their sen ...

  6. 003---wsgi和wsgiref模块

    WSGI: 全称:Web Server Gatway Interface ,web服务网关接口,独立的,与django无关,他们俩只是遵循一个约定,是一个协议. wsgiref模块: 实现了WSGI协 ...

  7. [CodeForces950C]Zebras

    Description 题目地址: Codeforces 题意:给你一串只含01的字符串,判断能否将字符串分为k个子序列,使得子序列满足以下条件: 开头和结尾都是0 相邻的2个数是01或者10 如0, ...

  8. scala高级特性-01

    目标一:深入理解高阶函数 高阶函数 1.1概念 Scala混合了面向对象和函数式的特性, 我们通常将可以做为参数传递到方法中的表达式叫做函数. 在函数式编程语言中,函数是“头等公民”, 高阶函数包含: ...

  9. 19,Ubuntu安装之python开发

      什么??公司要用Ubuntu(乌班图)?不会用??怎么进行python开发??? 乌班图操作系统下载地址:http://releases.ubuntu.com/18.04/ubuntu-18.04 ...

  10. CentOS 6.0 VNC远程桌面配置[转]

    原文出处: http://blog.haohtml.com/archives/12281 谢谢作者. 引言:必须明白:vncserver在调用的时候,会根据你的配置来启用server端的监听端口,端口 ...