python和node.js生成当前时间戳的示例

Danica ·
更新时间:2024-09-20
· 682 次阅读

Python

# coding=utf-8 import time import sys def func(): # 格式化输出时间 s1 = int(time.strftime("%Y%m%d%H%M%S", time.localtime())) # 时间戳,由于默认是秒需要转换为毫秒输出 s2 = int(round(time.time() * 1000)) return s1, s2 def once(): '''如果没有指明命令行参数则运行一次''' s1, s2 = func() print(s1) print(s2) def main(): args = sys.argv # 获取命令行参数 if len(args) > 1: count = args[1] # 命令行参数为数字,则生成指定数量的时间戳 if count.isdigit() and int(count) > 1: s1, s2 = func() # 元组解构 # 按参数指定的次数递增时间 for i in range(int(count)): print(s1 + i) print(s2 + i) else: once() else: once() if __name__ == "__main__": main()

按指定次数生成,在命令行中执行,如下命令是指定生成10个

python app.py 10

node.js

这段代码只能使用NodeJS环境来运行,需要先安装NodeJS

function func() { const dt = new Date(); // 按年月日时分秒的顺序存入数组 const source = [dt.getFullYear(), dt.getMonth() + 1, dt.getDate(), dt.getHours(), dt.getMinutes(), dt.getSeconds()]; let t = source[0]; // 第一位为年份,从月份开始拼接时间数字,月份、天数、小时等如果是一位数字,会补上0,保证显示为两位 for (let i = 1; i < source.length; i++) { const element = source[i]; t = t * 100 + element; } // Date.now为Date对象的静态方法,可以直接获取到时间戳 return [t, Date.now()]; // 返回的是一个数组,第一个为年月日时分秒的数字,第二个为时间戳 } /* 如果没有指明命令行参数则运行一次 */ function once() { const [s1, s2] = func(); console.log(s1); console.log(s2); } // NodeJS中获取命令行参数(process.argv) // 若要在普通的JS环境中运行(如浏览器),需要去掉对这个特殊变量(process.argv)的处理 const args = process.argv; if (args.length > 2) { const len = Number(args[2]); if (!isNaN(len) && len > 1) { let [s1, s2] = func(); // 数组解构 // 根据命令行参数指定的次数来生成多个时间戳,递增 for (let i = 0; i < len; i++) { console.log(s1 + i); console.log(s2 + i); } } else { once(); } } else { once(); }

按指定次数生成,在命令行中执行,如下命令是指定生成10个

node app.js 10

如果能看到最后,或对你有帮助的话,欢迎在评论区留言一起交流。

以上就是python和node.js生成当前时间戳的示例的详细内容,更多关于python和node.js生成当前时间戳的资料请关注软件开发网其它相关文章!

您可能感兴趣的文章:python获取时间戳的实现示例(10位和13位)Python sqlalchemy时间戳及密码管理实现代码详解python如何快速生成时间戳python3 中时间戳、时间、日期的转换和加减操作python生成13位或16位时间戳以及反向解析时间戳的实例python时间与Unix时间戳相互转换方法详解nodejs如何获取时间戳与时间差



当前时间 node.js 示例 时间戳 node js Python

需要 登录 后方可回复, 如果你还没有账号请 注册新账号