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. TypeScript泛型类 - 把类作为参数类型的泛型类

    /* TypeScript泛型类 - 把类作为参数类型的泛型类 */ /* 泛类:泛型可以帮助我们避免重复的代码以及对不特定数据类型的支持(类型校验),下面我们看看把类当做参数的泛型类 1.定义个类 ...

  2. ISO/IEC 9899:2011 条款6.8.6——跳转语句

    6.8.6 跳转语句 语法 1.jump-statement: goto    identifier    ; continue    ; break    ; return    expressio ...

  3. ES6深入浅出-13 Proxy 与 Reflect-3.Vue 3 将用 Proxy 改写

    如果说想打印出来年龄,但是有没有年龄的这个key值 把创建年龄写在一个按钮上面 通过一个事件来做. 点击创建年龄的按钮,给obj.age设置为18,但是页面的双向绑定并没有显示出来. 因为不响应式,为 ...

  4. 【插件】thinkphp5+百度编辑器自定义上传

    1 官方下载sdk 2 在引入编辑器页面.写入js // 百度编辑器 UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActi ...

  5. Linux strace追踪命令详解

    strace介绍 strace命令是一个集诊断.调试.统计与一体的工具,我们可以使用strace对应用的系统调用和信号传递的跟踪结果来对应用进行分析,以达到解决问题或者是了解应用工作过程的目的.当然s ...

  6. 05点睛Spring4.1-国际化

    5.1 ReloadableResourceBundleMessageSource 使用ReloadableResourceBundleMessageSource可获得不同语言的配置 此处是全局配置, ...

  7. SpringBoot学习笔记:单元测试

    SpringBoot学习笔记:单元测试 单元测试 单元测试(英语:Unit Testing)又称为模块测试,是针对程序模块(软件设计的最小单位)来进行正确性检验的测试工作.程序单元是应用的最小可测试部 ...

  8. Andrew Ng机器学习课程10

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

  9. 网络I/O模型

    事件驱动模型 与传统编程模式不同,事件驱动程序在启动之后,就在那等待,等待什么呢?等待被事件触发.传统编程下也有“等待”的时候,比如在代码块D中,你定义了一个input(),需要用户输入数据.但这与下 ...

  10. Linux查看库依赖方法

    1.查看依赖的库:objdump -x xxx.so | grep NEEDED 2.查看可执行程序依赖的库:objdump -x 可执行程序名 | grep NEEDED 3.查看缺少的库: ldd ...