当前位置: 首页 > news >正文

javascript【格式化时间日期】

javascript【格式化时间日期】 操作:

(1) 日期格式化代码

/*** 日期格式化函数<br/>* 调用格式:需要使用日期对象调用* <p> new Date().Format("yyyy/MM/dd HH:mm:ss"); </p>* @param fmt 日期格式* @returns {*} 返回格式化后的日期* @constructor*/
Date.prototype.Format = function (fmt) {var object = {"M+": this.getMonth() + 1, // 月"d+": this.getDate(), // 日"H+": this.getHours(), // 时"m+": this.getMinutes(), // 分"s+": this.getSeconds(), // 秒"q+": Math.floor((this.getMonth() + 3) / 3), // 季度"S": this.getMilliseconds() // 毫秒};// 正则表达式if (/(y+)/.test(fmt)) {fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));}for (var pattern in object) {if (new RegExp("(" + pattern + ")").test(fmt)) {fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (object[pattern]) : (("00" + object[pattern]).substr(("" + object[pattern]).length)));}}return fmt;
}

(2) 函数调用

使用一个Date对象去调用Format函数即可。

时间戳转换,如果是时间戳需要转换,则需要注意的是,时间戳是10位长度的,还是13位长度的,*如果是10位长度的时间戳,那么就需要乘以1000后,在调用Format进行转换*

举例如下:

// 调用函数
// 年月日
var date1 = new Date().Format("yyyy/MM/dd")
console.log(date1)
// 年月日 时分秒 毫秒
var date2 = new Date().Format("yyyy/MM/dd HH:mm:ss.S")
console.log(date2)
// 年月日 时分秒 毫秒 季度
var date3 = new Date().Format("yyyy/MM/dd HH:mm:ss.S qq")
console.log(date3)
// 时间戳
var date4 = new Date(1609430400000).Format("yyyy/MM/dd HH:mm:ss.S qq")
console.log(date4)

代码运行结果如下:

在这里插入图片描述

http://www.lryc.cn/news/163978.html

相关文章:

  • CCC数字钥匙设计【NFC】--什么是AID?
  • 变压器耐压试验电压及电源容量的计算
  • uniapp实现底部弹出菜单选择
  • 14. 线性代数 - 线性方程组
  • C++QT day4
  • Python中的 if __name__ ==‘main‘
  • github 创建自己的分支 并下载代码
  • 算法:贪心---跳一跳
  • 机器学习入门教学——梯度下降、梯度上升
  • BUUCTF Reverse/[羊城杯 2020]login(python程序)
  • indexDB localForage
  • Spring Boot开发时Java对象和Json对象互转
  • C++ 多态
  • LeetCode 之 二分查找
  • 【性能测试】中间件优化
  • 【算法】查找类——二分查找算法
  • Ansible FIle模块,使用Ansible File模块进行文件管理
  • 索尼mp4变成rsv修复案例(ILME-FX3)
  • 抓拍摄像机开关量控制4K高清手机远程看图建筑生长定时缩时相机
  • c++使用http请求-drogon框架
  • 幼儿棒球运动宣传介绍·野球6号位
  • grpc多语言通信之GO和DART
  • 基于FPGA的RGB图像转Ycbcr实现,包括tb测试文件以及MATLAB辅助验证
  • centos 编译安装的php多版本 切换
  • Unity 性能优化之Shader分析处理函数ShaderUtil.HasProceduralInstancing: 深入解析与实用案例
  • 2023数学建模国赛E题黄河水沙监测数据分析完整代码分析+处理结果+思路文档
  • 玩转Mysql系列 - 第19篇:游标详解
  • 【量化分析】Python 布林线( Bollinger)概念
  • MySQL的权限管理与远程访问
  • 在Qt创建的UI中放一个显示点云的窗口(PCL+QT5)