1. Thu Mar 07 2019 12:00:00 GMT+0800 (中国标准时间) 转换为 2019-03-07 12:00:00

  const d = new Date(Thu Mar 07 2019 12:00:00 GMT+0800 (中国标准时间))
const resDate = d.getFullYear() + '-' + this.p((d.getMonth() + 1)) + '-' + this.p(d.getDate())
const resTime = this.p(d.getHours()) + ':' + this.p(d.getMinutes()) + ':' + this.p(d.getSeconds())

p为不够10添加0的函数

  p(s) {
return s < 10 ? '0' + s : s
},

2.2019-03-07 12:00:00转换为 Thu Mar 07 2019 12:00:00 GMT+0800 (中国标准时间)

parserDate(date) {
var t = Date.parse(date)
if (!isNaN(t)) {
return new Date(Date.parse(date.replace(/-/g, '/')))
}
},

3.时间转时间戳

Thu Sep 20 2018 16:47:52 GMT+0800 (中国标准时间)转换为1537433272051

console.log(Date.parse(new Date()))

console.log(new Date().getTime())

"2018-09-20 16:50:48"转换为1537433448000

var timeDate = "2018-09-20 16:50:48";
var Time = new Date(timeDate);
var timestemp = Time.getTime();
console.log(timestemp)

 4.将日期转换为指定的格式:比如转换成 年月日时分秒 这种格式:yyyy-MM-dd hh:mm:ss 或者 yyyy-MM-dd

Date.prototype.format = function(fmt) {
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(fmt)) {
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(var k in o) {
if(new RegExp("("+ k +")").test(fmt)){
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
}
}
return fmt;
}

调用

var time1 = new Date().format("yyyy-MM-dd hh:mm:ss");
console.log(time1);

运行如下:

也可以转换成 ”年月日”的格式

var time2 = new Date().format("yyyy-MM-dd");
console.log(time2);

运行如下:

还将指定的日期转换为"年月日"的格式,代码如下:

    var oldTime = (new Date("2012/12/25 20:11:11")).getTime();
var curTime = new Date(oldTime).format("yyyy-MM-dd");
console.log(curTime);

运行如下:

还可以将 "时间戳" 转换为 "年月日" 的格式.

比如如下代码: 

    var da = 1402233166999;
da = new Date(da);
var year = da.getFullYear()+'年';
var month = da.getMonth()+1+'月';
var date = da.getDate()+'日';
console.log([year,month,date].join('-'));

												

js时间格式化和相互转换的更多相关文章

  1. js 时间格式化 (兼容safari)

    js 时间格式化,兼容IE8和safari浏览器. function formatDate(date, fmt, near, type) { var dateStr = date; if (!date ...

  2. js时间格式化函数,支持Unix时间戳

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  3. 时间戳显示为多少分钟前,多少天前的JS处理,JS时间格式化,时间戳的转换

    var dateDiff = function (timestamp) { // 补全为13位 var arrTimestamp = (timestamp + '').split(''); for ( ...

  4. 表单序列化json字符串和js时间格式化

    js时间格式化 new Date().format("时间格式") Date.prototype.format = function(fmt) { var o = {        ...

  5. js时间格式化

    const formatDate = timestamp => { const date = new Date(timestamp); const m = date.getMonth() + 1 ...

  6. js时间格式化(yy年MM月dd日 hh:mm)

    //时间格式化 Date.prototype.format = function (format) { var o = { "M+": this.getMonth() + 1, / ...

  7. js 时间格式化 -- 时间加减实现

    时间格式化的方法: Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.ge ...

  8. JS 时间格式化函数

    //时间格式化函数 Date.prototype.format = function (format) { var o = { "M+": this.getMonth() + 1, ...

  9. js时间格式化函数(兼容IOS)

    * 时间格式化 * @param {Object} dateObj 时间对象 * @param {String} fmt 格式化字符串 */ dateFormat(dateObj, fmt) { le ...

随机推荐

  1. tengine无法解析ssi报错 Nginx: unsafe URI detected while sending response

    Nginx: unsafe URI detected while sending response 现象:# 类似 <!--#include virtual="../library/h ...

  2. ISO/IEC 9899:2011 条款6.5.15——条件操作符

    6.5.15 条件操作符 语法 1.conditional-expression: logical-OR-expression logical-OR-expression    ?    expres ...

  3. 查询数据,从链接地址中爬取文章内容jsoup

    查询数据,从链接地址中爬取文章内容 protected void doGet(HttpServletRequest request, HttpServletResponse response) thr ...

  4. 将C++资源文件读取出来

    HRSRC   hResource   =   FindResource(GetModuleHandle(NULL),     MAKEINTRESOURCE(IDR_CALC),   TEXT(&q ...

  5. 123457123456#4#----com.MC.mathGame246----前拼后广--数学Q版训练Game-mc4444

    com.MC.mathGame246----前拼后广--数学Q版训练Game-mc

  6. Python3之内建模块datetime

    datetime datetime是python处理日期和时间的标准库 获取当前日期和时间 >>> from datetime import datetime #获取当前的datet ...

  7. Django:序列化的几种方法

    前言 关于序列化操作,就是将一个可迭代的数据结构,通过便利的方式进行我们所需要的操作. 今天历来归纳一下,Django中的几种不同得分方法,已经Django-restframework提供的方法 创建 ...

  8. beego框架(golang)学习验证码

    beego框架(golang)学习验证码 登录页面使用验证码 路由设置 /beego_admin_template/routers/router.go get请求页面, post验证用户名密码和验证码 ...

  9. Andrew Ng机器学习课程10

    Andrew Ng机器学习课程10 a example 如果hypothesis set中的hypothesis是由d个real number决定的,那么用64位的计算机数据表示的话,那么模型的个数一 ...

  10. PHP使用glob方法遍历文件夹下所有文件

    PHP使用glob方法遍历文件夹下所有文件 遍历文件夹下所有文件,一般可以使用opendir 与 readdir 方法来遍历.<pre><?php$path = dirname(__ ...