首页 > 其他分享 >将new Date获取到的时间转换格式

将new Date获取到的时间转换格式

时间:2023-10-19 18:55:52浏览次数:40  
标签:DD padStart t1 HH toString 格式 Date new

1.定义方法

    fDate(t1, format = 'YYYY-MM-DD HH:mm:ss') {
      const config = {
        YYYY: t1.getFullYear(),
        MM: (t1.getMonth() + 1).toString().padStart(2, '0'),
        DD: t1.getDate().toString().padStart(2, '0'),
        HH: t1.getHours().toString().padStart(2, '0'),
        mm: t1.getMinutes().toString().padStart(2, '0'),
        ss: t1.getSeconds().toString().toString().padStart(2, '0')
      };
      for (const key in config) {
        format = format.replace(key, config[key]);
      }
      return format;
    }

2.使用:

let times = this.fDate(new Date(), 'YYYY-MM-DD HH:mm:ss');
let time = this.fDate(new Date(), 'YYYY年MM月DD日 HH:mm:ss');

标签:DD,padStart,t1,HH,toString,格式,Date,new
From: https://www.cnblogs.com/domin520Jian/p/17775384.html

相关文章

  • python数据清洗日期格式和ipv4地址格式
    清洗日期格式importrefromdatetimeimportdatetime#读取文件withopen('result.txt','r')asfile:data=file.read()#使用正则表达式查找日期时间字符串pattern=r'(\d{2}/[A-Za-z]{3}/\d{4}:\d{2}:\d{2}:\d{2}\+\d{4})'matches=re.find......
  • Linux shell编程学习笔记4:修改命令行提示符格式(内容和颜色)
    一、命令行提示符格式内容因shell类型而异Linux终端命令行提示符内容格式则因shell的类型而异,例如CoreLinux默认的shell是sh,其命令行提示符为黑底白字,内容为:tc@box:/$其中,tc为当前用户名,box为主机名,/为当前目录路径,$表示当前用户类型是普通用户 。 二、环境变量PS1存储了命令行提......
  • 想让你的代码简洁,试试这个SimpleDateFormat类高深用法
    本文分享自华为云社区《从入门到精通:SimpleDateFormat类高深用法,让你的代码更简洁!》,作者:bug菌。环境说明:Windows10+IntelliJIDEA2021.3.2+Jdk1.8@[toc]前言日期时间在开发中是非常常见的需求,尤其是在处理与时间相关的业务逻辑时,我们需要对日期时间进行格式化、比较......
  • 关于 npoi 的 DateUtil.IsCellDateFormatted(cell) 为true,取cell.DateCellValue却报
    NPOI中数字和日期都是NUMERIC类型的,这里对其进行判断是否是日期类型所以当 DateUtil.IsCellDateFormatted为true时,理论是应该可以取到  cell.DateCellValue但实际上,cell.DateCellValue可能会报异常,而取 cell.NumericCellValue却是正常的,HSSFWorkbook是excel2007以前......
  • vue2和vue3导出页面为PDF格式:jspdf和html2canvas
    一、vue2导出PDF使用步骤1、安装html2canvas,将页面html转换成图片npminstall--savehtml2canvas卸载:npmuninstallhtml2canvas指定版本安装:[email protected]、安装jspdf,将图片生成pdfnpminstalljspdf--save3、定义全局函数在指......
  • macOS 支持 Raw 格式的相机名称 All In One
    macOS支持Raw格式的相机名称AllInOneRawSupport```sh$system_profiler$system_profiler>system_profiler.md#具体CPU型号信息隐藏了/加密了......
  • laravel:返回统一格式的json
    一,参考文档https://learnku.com/docs/laravel/10.x/responses/14850二,php代码1,App\extend\result\Result.php1234567891011121314151617181920212223242526272829303132333435<?php/*   统一格式的返回json数据*/na......
  • Excel如何在条件格式中利用函数
    1、规则简单的,可以选择单元格范围后,在条件格式工具条中选择相应规则即可。例如,可以对C5:C20设置条件格式空值,先选择C5:C20,然后在条件格式工具条中选择其它规则,再在编辑规则说明中的单元格值里面选择“空值”,然后设置好格式,确定即可。如下图: 2、对于需要利用函数的,则在选择C5单元......
  • [914] In Python's datetime library, you can format dates using the strftime() me
    InPython'sdatetimelibrary,youcanformatdatesusingthestrftime()method.Thismethodallowsyoutocreateaformattedstringrepresentationofadatetimeobject,specifyingtheformatyouwant.Here'showyoucanformatadateusingstrft......
  • 【MySQL】DATE_FORMAT,DATE_ADD函数用法
    一、示例1select*frombi.testwhereDATE_FORMAT(UPDATE_TIME,'%Y-%m-%d')='2023-09-11';当然 '%Y-%m-%d'是可以根据实际需求调整的  二、示例22.1给时间增加一小时UPDATEbi.testSETUPDATE_TIME=DATE_ADD(UPDATE_TIME,INTERVAL1HOUR);2.2给时间减少......