Skip to content
Advertisement

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

Please can anyone provide me function for time,date & day in python

just like time = time() and time = “##:##:##” # is replaced with current time in my laptop

And date = date() and date = “####/##/##”

And day = day() and day = “sun/mon/tue/wed/etc”

Advertisement

Answer

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()
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement