Python script get date and time All In One
Python shell script print current datetime to log file
# ✅ ? 相对路径 env
#!/usr/bin/env bash
# 仅仅适用于 Python ❌
# 指定文件编码 UTF-8
# coding: utf8
# ? 绝对路径, 存在错误风险
#!/usr/bin/bash
errors
Python !== Shell ❌
$ ./gpio.py 3
File "/home/pi/Desktop/./gpio.py", line 15
datetime = $(date '+%Y-%m-%d %T')
^
SyntaxError: invalid syntax
solutions
〉 Python get datetime
from datetime import datetime
# 获得当前时间
now = datetime.now()
# 转换为指定的格式
currentTime = now.strftime("%Y-%m-%d %H:%M:%S")
print('currentTime =', currentTime)
# currentTime = 2023-04-12 04:24:24
import datetime
# 获得当前时间
now = datetime.datetime.now()
# 转换为指定的格式
currentTime = now.strftime("%Y-%m-%d %H:%M:%S")
print('currentTime =', currentTime)
# currentTime = 2023-04-12 04:23:40
demos
Python
#!/usr/bin/env python3
# coding: utf8
import RPi.GPIO as GPIO
import time
import sys
arg1 = sys.argv[1]
print("arg1 =", arg1);
# 获取时间戳 ✅
# SH_DATE=$(TZ=':Asia/Shanghai' date '+%Y-%m-%d %T');
# datetime = $SH_DATE
print("⏰ current datetime =", datetime);
# $ pinout 命令查看,或 https://pinout.xyz/
# 指定 BCM 模式下的 GPIO 针脚编号是 12
# 对应的物理针脚编号是 32
PIN = 12
# BCM 模式
GPIO.setmode(GPIO.BCM)
# 指定 GPIO 针脚为一个电流输出针脚
GPIO.setup(PIN, GPIO.OUT)
# 输出低电平
GPIO.output(PIN, GPIO.LOW)
# index
i = 0
# max
# n = 7
# 类型转换,str => int
n = int(arg1)
print("n =", n)
print('开始闪烁⏳')
while (i < n):
print("i =", i)
# 高电平,LED 点亮
GPIO.output(PIN, GPIO.HIGH)
# 休眠 1 秒,防止 LED 长时间点亮烧坏了
time.sleep(1.0)
# 低电平,LED 熄灭
GPIO.output(PIN, GPIO.LOW)
# 休眠 1 秒
time.sleep(1.0)
i = i + 1
# 输出低电平,LED 关闭
# GPIO.output(PIN, GPIO.LOW)
# 清除,释放内存
GPIO.cleanup()
print('结束闪烁 ??')
(? 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
datetime & time
Python packages
https://docs.python.org/3/library/datetime.html
https://docs.python.org/3/library/time.html#module-time
# import datetime
from datetime import datetime
print("datatime =", datetime)
now = datetime.now()
print('⏰ now =', now)
https://www.programiz.com/python-programming/datetime
https://www.geeksforgeeks.org/python-datetime-module/
Python REPL
https://www.w3schools.com/python/trypython.asp?filename=demo_datetime1
Python 将时间戳转换为指定格式日期
import time
# 获得当前时间时间戳
now = int(time.time())
#转换为其他日期格式,如:"%Y-%m-%d %H:%M:%S"
timeArray = time.localtime(now)
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
print(otherStyleTime)
# 执行以上代码输出结果为:
2019-05-21 18:02:49
import datetime
# 获得当前时间
now = datetime.datetime.now()
# 转换为指定的格式
otherStyleTime = now.strftime("%Y-%m-%d %H:%M:%S")
print(otherStyleTime)
# 执行以上代码输出结果为:
2019-05-21 18:03:48
https://www.runoob.com/?s=python+date
https://www.runoob.com/python/att-time-strftime.html
https://www.runoob.com/python3/python-timstamp-str.html
https://www.runoob.com/python3/python3-get-yesterday.html
refs
https://www.cnblogs.com/xgqfrms/p/17308980.html
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 ?️,侵权必究⚠️!
- Python script date time Allpython script date time picker a-date-picker a-time-picker组件 element-plus default-time date-picker组件 axis time-axis date-axis时间 javascript date all gmt google script tools all scripts hooks npm all condition_variable time_duration notify_all input html date all script linux shell all