谁能给我一个函数来计算 python 中的日期和时间?我在 WINDOWS 10

Can anyone give me a function to calculate date and time in python ? I m in WINDOWS 10

谁能为我提供 python

中的时间、日期和日期功能

就像时间=时间() and time = "##:##:##" # 替换为笔记本电脑中的当前时间

并且日期=日期() 和日期 = "####/##/##"

天=天() 和天=“sun/mon/tue/wed/etc”

import subprocess as sp
import datetime as dt

def time():
    t = dt.datetime.now()
    h = str(int(t.strftime("%H")))
    m = str(int(t.strftime("%M")))
    s = str(int(t.strftime("%S")))
    ampm = " AM "
    h = int(h)
    if h > 12:
        h = h - 12
        ampm = " PM "
    h =str(h)
    return str(h) + ":" + t.strftime("%M:%S") + " " + ampm

def out(command,he):
    result = he.run(command, stdout=he.PIPE, stderr=he.PIPE, universal_newlines=False, shell=True)
    d = result.stdout
    outp = d
    f = d.find(b'\r')
    if f>0:
        outp = d[0:f]
        outp = str(outp)
        outp = outp[2:len(outp)-1]
    return outp

def date(han):
    myot = out("echo %date%",han)
    myot = myot[4:len(myot)]
    return myot

def day(he):
    myot = out("echo %date%",he)
    myot = myot[0:3]
    if myot == "Wed":
        myot = myot + "nes"
    elif myot == "Thu":
        myot = myot + "rs"
    elif myot == "Sat":
        myot = myot + "ur"
    elif myot == "Tue":
        myot = myot + "s"
    myot = myot + "day"
    return myot

date = date(sp)
day = day(sp)
time = time()

要获取包含当前时间和日期的时间戳,您可以使用:

import datetime

timestamp = datetime.datetime.now()

然后您可以使用该 timestamp 对象来获取您需要的信息:

  • timestamp.year
  • timestamp.month
  • timestamp.day
  • timestamp.hour
  • timestamp.minute
  • timestamp.second
  • timestamp.weekday() - 这是一个方法,所以你需要括号

如果您想更多地了解您在做什么,可以阅读 python 日期时间文档 https://docs.python.org/3/library/datetime.html