js中date类型的格式转化为yyyy-MM-dd HH:mm:ss的String类型
在vue中或其他框架中可以在Date的原型链中添加Format的方法,如ruoyi可以写在main.js中更好,如果写在utils还需要去导入包。
正常的js直接放到utils.js就好
Date.prototype.Format = function (formatStr) {
var str = formatStr;
var Week = ['日', '一', '二', '三', '四', '五', '六'];
str = str.replace(/yyyy|YYYY/, this.getFullYear());
str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));
var month = this.getMonth() + 1;
str = str.replace(/MM/, month > 9 ? month.toString() : '0' + month);
str = str.replace(/M/g, month);
str = str.replace(/w|W/g, Week[this.getDay()]);
str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
str = str.replace(/d|D/g, this.getDate());
str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours());
str = str.replace(/h|H/g, this.getHours());
str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());
str = str.replace(/m/g, this.getMinutes());
str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
str = str.replace(/s|S/g, this.getSeconds());
return str;
}
1.Date类型转化为固定的String格式
//创建日期
let newDate = new Date("Mon Mar 01 2021 11:33:32 GMT+0800 (中国标准时间)").Format("yyyy-MM-dd HH:mm:ss")
console.log(newDate) //2021-03-01 11:33:32 这个是String类型
2.固定的String格式转化为Date类型
let sd = "2021-03-01 11:33:32";
let array = t.split("-");
let dt = new Date(array[0], array[1], array[2]);
var dtt = new Date(t.replace("-g-/", ""));
console.log(dtt) //"Mon Mar 01 2021 11:33:32 GMT+0800 (中国标准时间)"
js中date类型的格式转化为yyyy-MM-dd HH:mm:ss的String类型的更多相关文章
- 小程序日期格式(yyyy-MM-dd HH:mm:ss)转(yyyy/MM/dd HH:mm:ss)
let newDate = (date).replace(/-/g, '/'); var date = new Date(newDate).getTime();
- js中Date的构造函数解读
javascript中的内置对象是我们经常会用到的,那么今天我们就来说说Date的四种构造方法吧 一.new Date() 这是我们最常使用也最熟悉不过的Date对象的构造方法了,通过无参数的构造函数 ...
- JS中date日期初始化的5种方法
创建一个日期对象: 代码如下: var objDate=new Date([arguments list]); 参数形式有以下5种: 1)new Date("month dd,yyyy hh ...
- javascript 的Date 格式化, 模仿shell中date命令的格式
原文:javascript 的Date 格式化, 模仿shell中date命令的格式 shell 中显示当前的日期 [root@localhost]$ date '+%Y-%m-%d %H:%M:%S ...
- JS高级面试题思路(装箱和拆箱、栈和堆、js中sort()方法、.js中Date对象中的getMounth() 需要注意的、开发中编码和解码使用场景有哪些)
1.装箱和拆箱: 装箱:把基本数据类型转化为对应的引用数据类型的操作: var num = 123 // num var objNum = new Num(123) // object console ...
- Oracle中把一个DateTime的字符串转化成date类型。to_date('2016/12/8 18:55:43','yyyy/MM/dd hh24:mi:ss'),
Oracle中把一个DateTime或者该形态字符串转化成date类型. to_date('2016/12/8 18:55:43','yyyy/MM/dd hh24:mi:ss'), 或者: sele ...
- js实现小时钟,js中Date对象的使用?
介绍一下js中Date对象的使用 dateObj = new Date() dateObj = new Date(dateValue) dateObj = new Date(year,month,da ...
- 序列化之二(将"\/Date(942289871000)\/"格式的时间替换成"yyyy-MM-dd HH:mm:ss"格式)
序列化就是一种用来处理对象流的机制.所谓对象流也就是将对象的内容进行流化,流的概念这里不用多说(就是I/O).我们可以对流化后的对象进行读写 操作,也可将流化后的对象传输于网络之间(注:要想将对象传输 ...
- java 日期格式转换EEE MMM dd HH:mm:ss z yyyy
SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale. ...
随机推荐
- [1.6W字] 浏览器跨域请求限制的详细原理分析&寻找一种最简单的方式实现XHR跨域(9种方法, 附大招可以纯前端实现跨域!)
Title/ 浏览器跨域(CrossOrigin)请求的原理, 以及解决方案详细指南 #flight.Archives011 序: 最近看到又有一波新的创作活动了, 官方给出的话题中有一个" ...
- zlib开发笔记(三):zlib库介绍、在ubuntu上进行arm平台交叉编译
前言 方便做嵌入式arm的交叉移植zlib库. Zlib库 zlib被设计为一个免费的,通用的,法律上不受限制的-即不受任何专利保护的无损数据压缩库,几乎可以在任何计算机硬件和操作系统上使 ...
- java小白困惑的那些事
刚接触java时,有些技术盲区,查了很多资料也得不到答案,面试时也得遮遮掩掩,这里就列举一些,当年踩过的那些坑 1.http -> https一个网站或接口,从http改到https是否需要额外 ...
- 【AI】TorchVision_DataLoad
From: https://liudongdong1.github.io/ All datasets are subclasses of torch.utils.data.Dataset i.e, t ...
- C# WCF的POST请求包含Stream及多个参数
当使用WCF的API的POST请求时,如果参数列表里,除了Stream类型形参,还具有多个形参, 在寄宿过程中会报错: 约定"IService1"中的操作"DoWork& ...
- VMware中安装CentOS Linux release 7.4.1708 (Core)
本篇文章主要介绍了VMware安装Centos7超详细过程(图文),具有一定的参考价值,感兴趣的小伙伴们可以参考一下 1.软硬件准备 软件:推荐使用VMwear,我用的是VMwear 12 镜像:Ce ...
- MySQL时间戳、字符串、日期
1.时间转字符串:date_format(date, format) SELECT date_format(now(), '%Y-%m-%d') 2.时间转时间戳:unix_timestamp() S ...
- freeswitch简介
freeswitch简介 freeswitch是开源的,免费的. freeswitch是一款非常好用的电话软交换框架,支持跨平台,扩展性良好,配置灵活. freeswitch可以在很多平台上运行,包括 ...
- 删除mysql数据库后django重建数据库
问题:由于表的结构设计的不太合理,后来要添加列,但是在django中使用makemigrations一直失败. 解决:索性就把mysql中对于django的数据库删了(其实也不用删除),在django ...
- Spring 事务回滚机制详解
1:事务原理 1.2:aop/动态代理 类路径:org/springframework/aop/framework/CglibAopProxy.java ReflectiveMethodInvocat ...