1. formatNumber = n => {
  2. n = n.toString();
  3. return n[1] ? n : '0' + n
  4. };
  5. // 时间格式化
  6. formatTime = date => {
  7. const year = date.getFullYear();
  8. const month = date.getMonth() + 1;
  9. const day = date.getDate();
  10. const hour = date.getHours()
  11. const minute = date.getMinutes()
  12. const second = date.getSeconds()
  13. return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  14. };

使用方法:

  let date = new Date("2020-6-8")

  let newDate = formatTime(date)  //  or formatTime(new Date())

  console.log(newDate, "==============")

  // 2020-06-08 ===============

JS时间格式转成字符串的更多相关文章

  1. Javascript里面的时间处理:将时间戳或时间对象转成字符串格式

    问题背景:想把一个时间直接转成字符串格式 通过查api发现有个toLocaleString(),根据本地时间格式,把 Date 对象转换为字符串 new Date().toLocaleString() ...

  2. js 时间格式转换

    js时间格式转换 格式化时间转成时间戳 //格式化转时间戳(单位秒) function strtotime(strtime) { strtime = strtime.substring(0, 19); ...

  3. JS时间格式CST转GMT

    转换方法:JS 时间格式CST转GMT 时区和Date:Java中的Date和时区转换

  4. Java之格林威治时间格式转换成北京时间格式

    Java之格林威治时间格式转换成北京时间格式 package com.mtons.mblog; import java.text.ParseException; import java.text.Si ...

  5. moment.js 时间格式转换

    moment.js 时间格式转换 moment.js 时间转化 bug 格式错误 bug 02:00 => 14:00 format HH 与 hh HH === 24 小时制 hh === 1 ...

  6. C#返回时间格式转换成 js 字符串

    在.net 中,调用 post 或者 get和后台通信时,如果有时间返回信息,后台返回的时间信息一般是这样格式:Thu Jul 9 23:14:53 UTC+0800 2015,那么要在前台显示就会有 ...

  7. JS时间格式 GMT格式转换

    JavaScript时间格式转换总结 1.当前系统区域设置格式(toLocaleDateString和toLocaleTimeString) 例子:(new Date()).toLocaleDateS ...

  8. JS 时间格式CST转GMT

    近几天,在做百度地图时,需要转换时间格式并做显示,但是发现显示的时间格式,出现了错乱,二者的日期和小时都出现了变动.例如: 原始时间格式:Thu Aug 18 20:38:54 CST 2016 转换 ...

  9. JS时间格式和时间戳的相互转换

    时间戳转化为日期的方式 ; var newDate = new Date(); newDate.setTime(timestamp * ); // Mon May 28 2018 console.lo ...

随机推荐

  1. Warm up HDU - 4612 树的直径

    题意:给出n个点和m条边的无向图,存在重边,问加一条边以后,剩下的桥的数量最少为多少. 题解: 你把这个无向图缩点后会得到一个只由桥来连接的图(可以说这个图中的所有边都是桥,相当于一棵树),然后我们只 ...

  2. hdu5375 Gray code

    Problem Description The reflected binary code, also known as Gray code after Frank Gray, is a binary ...

  3. 牛客编程巅峰赛S1第5场 - 青铜&白银 A.凯撒密码(字符串)

    题意:给你一个加密的字符串,以及偏移量,求对应的明文. 题解:根据样例,不难看出模板串是:\(0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopq ...

  4. Nestjs入门学习教程

    初次接触Nest,有问题欢迎指出: 简介 NestJS是一个用于构建高效.可扩展的Node.js服务器端应用程序的开发框架.简单来说是一款Node.js的后端框架. 它利用JavaScript的渐进增 ...

  5. Python 相关

    循环使用迭代器 Python中使用for循环时,内置容器的默认循环都是有迭代器的.使用迭代器每次只会读出一小部分到内存,不断往后next. 通常建议在没有必要的情况下不使用for key,value ...

  6. IIS6.0(CVE-2017-7269) 缓冲器溢出

    漏洞描述开启WebDAV服务对IIS 6.0存在缓冲区溢出漏洞都可以导致远程代码执行,所以对于目前的IIS 6.0用户而言,可用的变通方案就是关闭WebDAV服务. 漏洞编号CVE-2017-7269 ...

  7. React Hooks: useReducer All In One

    React Hooks: useReducer All In One useReducer https://reactjs.org/docs/hooks-reference.html#usereduc ...

  8. React PureComponent All In One

    React PureComponent All In One import React, { // useState, // useEffect, // Component, PureComponen ...

  9. cookie all in one

    cookie all in one credentials: "include" https://developers.google.com/web/updates/2015/03 ...

  10. free online linux terminal & github cli online

    free online linux terminal & github cli online gitpod https://www.gitpod.io/features/ https://bc ...