在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. 树莓派远程连接工具SSH使用教程

    树莓派远程连接工具SSH使用教程 树莓派 背景故事 树莓派作为一款迷你小主机,大部分的使用场景都会用到远程调试,远程调试用到最多的方式一般就是VNC和SSH,SSH就是命令行型的远程方式,简单来说就是 ...

  2. JVM的GC机制

    JVM的GC机制 1. 什么对象会被回收 引用计数法:如果一个对象被引用一次,则记录引用次数加一,如果引用取消,则减一,当减到0时,需要被回收. 问题:循环引用,A引用B,B引用A,除此之外,已经无法 ...

  3. cpu设计实践1

    本栏目将实现一个简单cpu(8-32位)的设计,使用xinlink spatan6平台

  4. 攻防世界PWN简单题 level2

    攻防世界PWN简单题 level2 此题考验的是对ROP链攻击的基础 万事开头PWN第一步checksec 一下 32位的小端程序,扔进IDA 进入函数,找出栈溢出漏洞. 又是这个位置的栈溢出,rea ...

  5. ES6中新增的数组知识记录

    JSON数组格式转换 let json = { '0': 'hello', '1': 'I am ', '2': 'michael', length:3 } 这就是一个JSON数组格式,跟普通的JSO ...

  6. spring-cloud-sleuth+zipkin追踪服务

    1, 父Maven pom 文件 <parent> <groupId>org.springframework.boot</groupId> <artifact ...

  7. python画循环圆

    import turtle for i in range(100,0,-5): # 从100到0循环递减每次减5 turtle.circle(i,90) 不懂为啥第一次运行会出错,错了再运行一遍for ...

  8. Maven解决依赖冲突

    依赖冲突 若项目中多个Jar同时引用了相同的Jar时,会产生依赖冲突,但Maven采用了两种避免冲突的策略,因此在Maven中是不存在依赖冲突的. 短路优先 本项目-->A.jar-->B ...

  9. Node.js开发博客系统

    数据库设计 用户表: id phone password nickname head_img personal_sign level_id create_time update_time is_del ...

  10. Metasploit用法详解

    Metasploit简介 1. Auxiliaries(辅助模块) 该模块不会直接在测试者和目标主机之间建立访问,它们只负责执行扫描.嗅探.指纹识别等相关功能以辅助渗透测试. 2. Exploit(漏 ...