在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类型的更多相关文章

  1. 小程序日期格式(yyyy-MM-dd HH:mm:ss)转(yyyy/MM/dd HH:mm:ss)

    let newDate = (date).replace(/-/g, '/'); var date = new Date(newDate).getTime();

  2. js中Date的构造函数解读

    javascript中的内置对象是我们经常会用到的,那么今天我们就来说说Date的四种构造方法吧 一.new Date() 这是我们最常使用也最熟悉不过的Date对象的构造方法了,通过无参数的构造函数 ...

  3. JS中date日期初始化的5种方法

    创建一个日期对象: 代码如下: var objDate=new Date([arguments list]); 参数形式有以下5种: 1)new Date("month dd,yyyy hh ...

  4. javascript 的Date 格式化, 模仿shell中date命令的格式

    原文:javascript 的Date 格式化, 模仿shell中date命令的格式 shell 中显示当前的日期 [root@localhost]$ date '+%Y-%m-%d %H:%M:%S ...

  5. JS高级面试题思路(装箱和拆箱、栈和堆、js中sort()方法、.js中Date对象中的getMounth() 需要注意的、开发中编码和解码使用场景有哪些)

    1.装箱和拆箱: 装箱:把基本数据类型转化为对应的引用数据类型的操作: var num = 123 // num var objNum = new Num(123) // object console ...

  6. 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 ...

  7. js实现小时钟,js中Date对象的使用?

    介绍一下js中Date对象的使用 dateObj = new Date() dateObj = new Date(dateValue) dateObj = new Date(year,month,da ...

  8. 序列化之二(将"\/Date(942289871000)\/"格式的时间替换成"yyyy-MM-dd HH:mm:ss"格式)

    序列化就是一种用来处理对象流的机制.所谓对象流也就是将对象的内容进行流化,流的概念这里不用多说(就是I/O).我们可以对流化后的对象进行读写 操作,也可将流化后的对象传输于网络之间(注:要想将对象传输 ...

  9. java 日期格式转换EEE MMM dd HH:mm:ss z yyyy

    SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale. ...

随机推荐

  1. CVE-2020-17523:Apache Shiro身份认证绕过漏洞分析

    0x01 Apache Shiro Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理. 0x02 漏洞简介 2021年2月1日,Apache Shiro官 ...

  2. 007 GMII、SGMII和SerDes的区别和联系

    一.GMII和SGMII的区别和联系 GMII和SGMII区别,上一篇已经介绍了,这一篇重点介绍SGMII和SerDes区别. GMII和SGMII GMII 在MII接口基础上提升了数据位宽和Clo ...

  3. noip18

    T1 来自cf原题 考场直接暴力枚举 \(A,B\),15pts. 正解: 首先时间的表达式,\(T=\frac{A}{a_{i}}+\frac{B}{b_{i}}\),然后以\(\frac{1}{a ...

  4. Java社区——个人项目开发笔记(二)

    1.B\S架构通信原理 浏览器,服务器之间产生通信,浏览器访问服务器,服务器返回一个HTML,浏览器会对HTML进行解析,并渲染相关的内容. 在解析过程中,会发现HTML里引用了css文件,js文件, ...

  5. taro小程序展示富文本

    在微信小程序下会用到wxParse这个东西来达到html转换wxml的效果, taro小程序官方也给出了示例,地址 这里封装成自己的组件: import Taro, { Component } fro ...

  6. mysql几种连接方式区别

    mysql的几种join 2017年03月19日 14:49:07 carl-zhao 阅读数:7845 标签: mysqlsqljoin 更多 个人分类: MySQL 版权声明:本文为博主原创文章, ...

  7. springboot中添加事务注解

    1.首先在service层中的方法前添加@Transactional @Service public class UserService { @Autowired private UserMapper ...

  8. mysql ORDER BY 中文出现错误问题

    在MySQL中,我们经常会对一个字段进行排序查询,但进行中文排序和查找的时候,对汉字的排序和查找结果往往都是错误的. 这种情况在MySQL的很多版本中都存在. 如果这个问题不解决,那么MySQL将无法 ...

  9. Docker安装MySQL集群【读写分离】

    Centos7系统Docker安装 目录 Centos7系统Docker安装 目录 Centos7系统Docker安装 1.下载mysql镜像 2.创建Master实例并启动 参数说明 3.创建Sla ...

  10. Tomcat集群Cluster实现原理

    1.Tomcat集群         Tomcat集群的问题之一是如何处理Session,Session是有状态的,请求到了Tomcat,后续流传是要根据上下文(Context)来进行的.我们可以改造 ...