JS获取当前时间戳与时间戳转日期时间格式

article/2025/9/21 5:57:26

一、获取当前时间的时间戳(三种方式)

const t1 = new Date().valueOf() // 第一种,推荐
const t2 = new Date().getTime() // 第二种,推荐
const t3 = Date.parse(new Date()) // 第三种,不推荐,精度差一些

在这里插入图片描述

注: new Date()得到的是一个时间对象

const times = new Date() // Sat Apr 16 2022 11:07:38 GMT+0800 (中国标准时间)

在这里插入图片描述

二、获取指定日期,时间的时间戳

const t = new Date('日期时间').valueOf() // 方法一
const t1 = new Date('日期时间').getTime() // 方法二
const t2 = new Date('2022-04-15').valueOf() // 1649980800000
const t3 = new Date('2022-04-15 12:15:36').valueOf() // 1649996136000
const t4 = new Date('2022-04-15').getTime() // 1649980800000
const t5 = new Date('2022-04-15 12:15:36').getTime() // 

在这里插入图片描述

三、时间戳转日期时间(vue项目中)

1. 创建一个date.js文件 ( src/util/date.js)
在这里插入图片描述

// 给Date类添加了一个新的实例方法format
Date.prototype.format = function (fmt) {// debugger;let o = {'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 (let k in o) {if (new RegExp('(' + k + ')').test(fmt)) {fmt = fmt.replace(RegExp.$1,RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))}}return fmt
}
// date: 时间对象, pattern: 日期格式
export function formatterDate (date, pattern) {let ts = date.getTime()let d = new Date(ts).format('yyyy-MM-dd hh:mm:ss') // 默认日期时间格式 yyyy-MM-dd hh:mm:ssif (pattern) {d = new Date(ts).format(pattern)}return d.toLocaleString()
}

2. 组件里面引入
在这里插入图片描述

3. 作为过滤器使用

<template><div><p>日期时间: {{times | formatterTime('yyyy-MM-dd hh:mm:ss')}}</p><p>日期: {{times | formatterTime('yyyy-MM-dd')}}</p><p>日期: {{times | formatterTime('yyyy年MM月dd日')}}</p></div>
</template><script>
import { formatterDate } from '@/util/date.js'
export default {data() {return {times: new Date().valueOf()// 获取当前时间戳}},filters: {formatterTime(val,type) { // val: 时间戳 (val是通道数据 即过滤器前面的数据,type是过滤器函数传递的参数)if (!val) return nullconst t = new Date(val)return formatterDate(t, type) // 日期时间}}
}
</script>

页面效果如下图:
在这里插入图片描述


http://chatgpt.dhexx.cn/article/Yi8dURGQ.shtml

相关文章

前端获取当前时间戳的3+2+1种方法

首先说下&#xff0c;三种比较常见的方法 1.Date.parse(new Date()) Date.parse(new Date()) 1656038612000 2.new Date().getTime() new Date().getTime() 1656038636915 3.new Date().valueOf() new Date().valueOf() 1656038660413 两种不常见的方法 1.Date.now() //i…

js获取当前时间、获取当前时间戳、时间与时间戳互转。

1、 JavaScript 获取当前时间戳&#xff1a; 第一种&#xff1a; var timestamp Date.parse(new Date()); console.log(timestamp); // 1622427159000第二种&#xff1a; var timestamp (new Date()).valueOf(); console.log(timestamp); // 1622427218359第三种&#xff1…

Python中tqdm模块的用法

参考 python的Tqdm模块 - 云社区 - 腾讯云 一、简介 tqdm是Python中专门用于进度条美化的模块&#xff0c;通过在非while的循环体内嵌入tqdm&#xff0c;可以得到一个能更好展现程序运行过程的提示进度条&#xff0c;本文就将针对tqdm的基本用法进行介绍。 二、基本用法 tqd…

tqdm简介及使用方法

tqdm源自阿拉伯语 taqaddum (تقدّم) &#xff0c;意思是进程( “progress”)&#xff0c;也是西班牙语中 “I love you so much” (te quiero demasiado)的缩写&#xff0c;听着就浪漫&#xff5e;。 该模块的作用就是使循环(loop)过程展示一个进度条&#xff0c;能够通过…

python中tqdm用法

tqdm安装命令&#xff1a; pip install tqdm 1.用tqdm子模块 &#xff08;1&#xff09;例子1 from tqdm import tqdm import timefor i in tqdm(range(50)):time.sleep(0.1)pass 结果如下&#xff1a; &#xff08;2&#xff09;例子2&#xff1a;带参数 from tqdm impo…

python进度条 tqdm使用教程

文章目录 安装tqdm在循环中显示进度条1. 使用tqdm.tqdm()实现进度条2. tqdm()主要参数默认值与解释3. tqdm()的返回值4. tqdm.trange()5. 循环过程中输出额外内容6. 无法获取迭代器长度的坑 *其他方法实现进度条 安装tqdm tqdm是一个显示进度条的python工具包。安装命令如下 …

DMQ简介

DMQ简y介 DMQ简介 - Water2Wine - 博客园 原文 1. DMQ简介 Distributed Message Queue&#xff08;分布式消息队列&#xff09;&#xff0c;DMQ 是在kafka之上做了一层封装&#xff0c;为AC的其他组件提供分布式消息队列服务。简单来说&#xff0c;DMQ就是一个消息中间件&a…

tqdm介绍

tqdm是一个进度条可视化库&#xff0c;可以帮助我们监测程序运行的进度&#xff0c;估计运行的时长&#xff0c;甚至可以协助debug。它是一个快速&#xff0c;可扩展的Python进度条&#xff0c;可以在 Python 长循环中添加一个进度提示信息&#xff0c;用户只需要封装任意的迭代…

python进度条神器之tqdm包的使用

前言&#xff1a;在Python开发中&#xff0c;我们经常需要使用进度条来展示任务进行的进度到哪里了&#xff0c;特别是在机器学习与深度学习框架里面&#xff0c;tqdm包是一个非常简单但是功能强大的进度条处理工具&#xff0c;一起来看看它的一些基本用法吧&#xff01; 一、…

Python中tqdm模块介绍

tqdm模块简单介绍 tqdm是一个可以显示Python进度条的模块&#xff0c;可通过pip在终端安装 pip install tqdm可通过 import tqdm 导入&#xff0c;也可通过 from tqdm import 【函数名】 导入 1. tqdm.tqdm 传入数字list: from tqdm import tqdm for i in tqdm(range(1, 5)…

Python进度条tqdm详细介绍

前言 有时候在使用Python处理比较耗时操作的时候&#xff0c;为了便于观察处理进度&#xff0c;这时候就需要通过进度条将处理情况进行可视化展示&#xff0c;以便我们能够及时了解情况。这对于第三方库非常丰富的Python来说&#xff0c;想要实现这一功能并不是什么难事。 tq…

【python第三方库】tqdm简介

转载自&#xff1a; https://blog.csdn.net/wxd1233/article/details/118371404 仅作学习记录&#xff0c;侵删~ 文章目录 介绍安装使用方法1.传入可迭代对象使用trange 2.为进度条设置描述3.手动控制进度4.tqdm的write方法5.手动设置处理的进度6.自定义进度条显示信息 在深度学…

【tqdm】进度条工具

tqdm 是一种呈现循环进度的工具包&#xff0c;本文讲讲述他的常用用法。 Example 1 假设FL框架中一共有10个用户&#xff0c;每次随机抽取5个用户进行更新&#xff0c;一共训练6个epochs&#xff0c;结合tqdm显示训练进程的代码框架如下&#xff1a; for epoch in range(6):…

tqdm模块

首先给大家看看tqdm源码中的一段注释&#xff1a; Decorate an iterable object, returning an iterator which acts exactly like the original iterable, but prints a dynamically updating progressbar every time a value is requested. 如果翻译过来&#xff1a; tqdm是…

python库tqdm是什么以及怎么用tqdm、trange和tqdm.notebook

1.是什么&#xff1f; 答案&#xff1a;用来显示进度条以及展示每一轮&#xff08;iteration)所耗费的时间。好抽象&#xff0c;我们重点看怎么用&#xff0c;从而明白是什么。 2.怎么用&#xff1f; 自行安装&#xff0c;如果是anaconda的话这个是默认已经安装了的。从而导入…

Py之tqdm:tqdm库的简介、安装、使用方法详细攻略

Py之tqdm&#xff1a;tqdm库的简介、安装、使用方法详细攻略 目录 tqdm库的简介 tqdm库的安装 tqdm库的使用方法 tqdm库的简介 显示循环的进度条的库。taqadum, تقدّم&#xff09;在阿拉伯语中的意思是进展。tqdm可以在长循环中添加一个进度提示信息&#xff0c;用户只…

tqdm安装

环境&#xff1a;win10 Python3.6 首先&#xff0c;直接使用pip安装&#xff1a;pip install tqdm &#xff0c;成功安装&#xff0c;但是导入不了tqdm进行使用。 import tqdm from tqdm import tqdm Traceback (most recent call last):File "D:\Anaconda3\lib\site-…

【PyTorch总结】tqdm的使用

文章目录 介绍安装使用方法1.传入可迭代对象使用trange2.为进度条设置描述3.手动控制进度4.tqdm的write方法5.手动设置处理的进度6.自定义进度条显示信息 在深度学习中如何使用 介绍 Tqdm 是 Python 进度条库&#xff0c;可以在 Python 长循环中添加一个进度提示信息。用户只需…

Python的Tqdm模块——进度条配置

tqdm官网地址&#xff1a;https://pypi.org/project/tqdm/ Github地址&#xff1a;https://github.com/tqdm/tqdm 简介 Tqdm 是一个快速&#xff0c;可扩展的Python进度条&#xff0c;可以在 Python 长循环中添加一个进度提示信息&#xff0c;用户只需要封装任意的迭代器 tqd…

tqdm 简介及正确的打开方式

tqdm 简介及正确的打开方式 查遍了网上资料&#xff0c;发现绝大中文的讲解全都是一手带过&#xff0c;还称详细&#xff0c;真是醉了&#xff0c;于是有该文&#xff0c;也算给自己做个笔记 1. 什么是tqdm&#xff1f; tqdm是一个快速的&#xff0c;易扩展的进度条提示模块&a…