SoFunction
Updated on 2024-11-19

python implementation of a simple date utility class

In this article, the example for you to share the python implementation of a simple date tool class specific code for your reference, the details are as follows

import datetime
import time

DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
TIME_FORMAT = "%H:%M:%S"

# Current milliseconds
def curMilis():
  return int(() * 1000)

# Current seconds
def curSeconds():
  return int(())

# Current date Format %Y-%m-%d %H:%M:%S
def curDatetime():
  return ((),DATETIME_FORMAT)

# Current date Format %Y-%m-%d
def curDate():
  return ()

# Current time Format %Y-%m-%d
def curTime():
  return (TIME_FORMAT)

#Seconds to date
def secondsToDatetime(seconds):
  return (DATETIME_FORMAT,(seconds))

# milliseconds to date
def milisToDatetime(milix):
  return (DATETIME_FORMAT,(milix//1000))

# Date to milliseconds
def datetimeToMilis(datetimestr):
  strf = (datetimestr,DATETIME_FORMAT)
  return int((strf)) * 1000

# Date to seconds
def datetimeToSeconds(datetimestr):
  strf = (datetimestr,DATETIME_FORMAT)
  return int((strf))

# Current year
def curYear():
  return ().year
# Current month
def curMonth():
  return ().month

# Current day
def curDay():
  return ().day

# Current time
def curHour():
  return ().hour

# Current minutes
def curMinute():
  return ().minute

# Current seconds
def curSecond():
  return ().second

# What day of the week
def curWeek():
  return ().weekday()

# A few days ago
def nowDaysAgo(days):
  daysAgoTime = () - (days = days)
  return (DATETIME_FORMAT,())

# A few days later
def nowDaysAfter(days):
  daysAgoTime = () + (days = days)
  return (DATETIME_FORMAT,())

# A few days before a certain date
def dtimeDaysAgo(dtimestr,days):
  daysAgoTime = (dtimestr,DATETIME_FORMAT) - (days = days)
  return (DATETIME_FORMAT,())

# A few days before a certain date
def dtimeDaysAfter(dtimestr,days):
  daysAgoTime = (dtimestr,DATETIME_FORMAT) + (days = days)
  return (DATETIME_FORMAT,())


secondStamp = curSeconds()
print("Current seconds:",secondStamp)
milisStamp = curMilis()
print("Current milliseconds:",milisStamp)

curdTime = curDatetime()
print("Current time:",curdTime)
curDate = curDate()
print("Current date:",curDate)
curT = curTime()
print("Current Moment:",curT)


stdtime = secondsToDatetime(secondStamp)
print("Second turnaround time:",stdtime)
mtdtime = milisToDatetime(milisStamp)
print("Millisecond turn times:",mtdtime)
dtimetm = datetimeToMilis(mtdtime)
print("Time to milliseconds:",dtimetm)
dtimets = datetimeToSeconds(mtdtime)
print("Time to Seconds:",dtimets)

year = curYear()
print("Year:",year)
month = curMonth()
print("Moon:",month)
day = curDay()
print("Day:",day)
hour = curHour()
print("Time:",hour)
minute = curMinute()
print("Points:",minute)
second = curSecond()
print("Seconds:",second)
week = curWeek()
print("Week:",week)

The output is as follows:

current second: 1518341913
Current milliseconds: 1518341913403
current time: 2018-02-11 17:38:33
current date: 2018-02-11
current moment: 17:38:33
parallax time: 2018-02-11 17:38:33
毫parallax time: 2018-02-11 17:38:33
Time to milliseconds: 1518341913000
time interval in seconds (e.g. seconds per second): 1518341913
surname Nian: 2018
moon: 2
date: 11
hour: 17
ingredient: 38
unit of angle or arc equivalent one sixtieth of a degree: 33
last week: 6
[Finished in 0.2s]

This is the entire content of this article.