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. Python3基础 函数 参数为list 使用+=会影响到外部的实参

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  2. python-learning-第二季-数据处理numpy

    https://www.bjsxt.com/down/8468.html numpy-科学计算基础库 例子: import numpy as np #创建数组 a = np.arange() prin ...

  3. ES6深入浅出-8 新版的类(下集)-1.简单语法

    回顾 当你声明一个空的对象obj的时候,会生成一块内存这个内存里面什么都没有,自由__proto__存在401的地址. 也就是Object的protototype在内存中的地址 类 通过函数创建类.这 ...

  4. DateUtil 提供一些常用的时间想法的方法

    package com.opslab.util; import java.text.ParseException;import java.text.SimpleDateFormat;import ja ...

  5. get请求utf-8解码

    package utils;import java.io.UnsupportedEncodingException;import java.net.URLDecoder; /*** <p> ...

  6. 安卓 android studio 报错 Unknown host 'jcenter.bintray.com'. You may need to adjust the proxy settings in Gradle.

    报错截图: 问题原因:因为build.gradle中jcenter()或者maven()被墙了,所以会出现这种情况. 解决方案:(我的gradle版本是:classpath 'com.android. ...

  7. 123457123457#0#----com.ppGame.YinYu45--前拼后广--儿童yinyu-pp

    com.ppGame.YinYu45--前拼后广--儿童yinyu-pp

  8. 一份 Tomcat 和 JVM 的性能调优经验总结!拿走不谢

    Tomcat性能调优 找到Tomcat根目录下的conf目录,修改server.xml文件的内容.对于这部分的调优,我所了解到的就是无非设置一下Tomcat服务器的最大并发数和Tomcat初始化时创建 ...

  9. Nginx限制ip访问

    首先建立下面的配置文件放在nginx的conf目录下面,命名为blocksip.conf: 加入以下代码: #屏蔽soso蜘蛛IP deny 113.108.12.154; #此为搜搜蜘蛛IP den ...

  10. (十四)访问标志 Access_flags

    一.概念 上一章节讲到了常量池,如下图,常量池之后便是访问标志acess_flags,占2个字节(u2). 二.例子 编写一个接口. public interface Test{ public fin ...