import time
import datetime
import locale
import random class TimeUtil: def __init__(self, curtime=None):
self.curtime = curtime def get_timestemp(self):
return time.time() def get_date(self):
return time.strftime("%Y-%m-%d") def get_time(self):
return time.strftime("%H:%M:%S") def get_datetime(self):
return time.strftime("%Y-%m-%d %H:%M:%S") def get_chinesedate(self):
locale.setlocale(locale.LC_ALL, 'en')
locale.setlocale(locale.LC_CTYPE, 'chinese')
strTime = time.strftime("%Y年%m月%d日", time.localtime())
return strTime def get_chinesetime(self):
locale.setlocale(locale.LC_ALL, 'en')
locale.setlocale(locale.LC_CTYPE, 'chinese')
strTime = time.strftime("%H时%M分%S秒", time.localtime())
return strTime def get_chinesedatetime(self):
locale.setlocale(locale.LC_ALL, 'en')
locale.setlocale(locale.LC_CTYPE, 'chinese') strTime = time.strftime("%Y年%m月%d日%H时%M分%S秒", time.localtime())
return strTime def compute_date(self, day_interval):
# 获取今天的日期
today = datetime.date.today()
# 在今天的日期上再减10天
if isinstance(day_interval, int) and day_interval >= 0:
return today + datetime.timedelta(days=day_interval)
elif isinstance(day_interval, int) and day_interval < 0:
return today - datetime.timedelta(days=abs(day_interval)) def timestamp_to_date(self, timestamp):
if not isinstance(timestamp, (int, float)):
return None
locale.setlocale(locale.LC_CTYPE, 'chinese')
time_tuple = time.localtime(timestamp) return str(time_tuple[0]) + "年" + str(time_tuple[1]) + "月" + str(time_tuple[2]) + "日" def timestamp_to_time(self, timestamp):
if not isinstance(timestamp, (int, float)):
return None
locale.setlocale(locale.LC_CTYPE, 'chinese')
time_tuple = time.localtime(timestamp)
return str(time_tuple[3]) + "时" + str(time_tuple[4]) + "分" + str(time_tuple[5]) + "秒" def timestamp_to_datetime(self, timestamp):
return self.timestamp_to_date(timestamp) + self.timestamp_to_time(timestamp) if __name__ == "__main__":
t = TimeUtil()
print(t.get_timestemp())
print(t.get_date())
print(t.get_time())
print(t.get_datetime())
print(t.get_chinesedate())
print(t.get_chinesetime())
print(t.get_chinesedatetime())
print(t.compute_date(10))
print(t.compute_date(-10))
print(t.timestamp_to_date(1333333333))
print(t.timestamp_to_time(1333333333))
print(t.timestamp_to_datetime(1333333333))

  打印效果

python时间戳,获取当前时间,时间格式转换,求出前几天或后几天的时间的更多相关文章

  1. Java练习 SDUT-2246_时间日期格式转换

    时间日期格式转换 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 对于日期的常用格式,在中国常采用格式的是"年 ...

  2. Java时间日期格式转换 转自:http://www.cnblogs.com/edwardlauxh/archive/2010/03/21/1918615.html

    Java时间格式转换大全 import java.text.*; import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * @ ...

  3. 使用new data计算时间以及格式转换

    1.时间计算,往后加30(默认一个月的时间),sxTime表示的是在当前时间往后加几天的之后一个月 function maxDate1(){ var nowDate = new Date(); max ...

  4. Java时间日期格式转换Date转String和String转Date

    Java时间格式转换大全 import java.text.*; import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * @ ...

  5. java 根据系统日期获取前一天、后一天时间(根据初始日期推算出期望(向前/向后)日期)

      1.情景展示  java 根据系统当前日期获取前一天日期.后一天日期,或者根据初始日期推算出期望(向前/向后)日期. 2.解决方案 导包 import java.text.ParseExcepti ...

  6. 在SQL Server中 获取日期、日期格式转换

    --常用日期转换参数: PRINT CONVERT(varchar, getdate(), 120 ) 2016-07-20 16:09:01 PRINT replace(replace(replac ...

  7. JAVA中获取当前系统时间及格式转换

    JAVA中获取当前系统时间   一. 获取当前系统时间和日期并格式化输出: import java.util.Date;import java.text.SimpleDateFormat; publi ...

  8. [js] js和C# 时间日期格式转换

    下午在搞MVC和EXTJS的日期格式互相转换遇到了问题,我们从.NET服务器端序列化一个DateTime对象的结果是一个字符串格式,如 '/Date(1335258540000)/' 这样的字串. 整 ...

  9. js和C# 时间日期格式转换

    下午在搞MVC和EXTJS的日期格式互相转换遇到了问题,我们从.NET服务器端序列化一个DateTime对象的结果是一个字符串格式,如 '/Date(1335258540000)/' 这样的字串. 整 ...

随机推荐

  1. NLP文本分类方法汇总

    模型: FastText TextCNN TextRNN RCNN 分层注意网络(Hierarchical Attention Network) 具有注意的seq2seq模型(seq2seq with ...

  2. 算法dfs——二叉搜索树中最接近的值 II

    901. 二叉搜索树中最接近的值 II 中文 English 给定一棵非空二叉搜索树以及一个target值,找到 BST 中最接近给定值的 k 个数. 样例 样例 1: 输入: {1} 0.00000 ...

  3. Kotlin匿名函数与闭包详解

    Lambda表达式实例演练: 继续先来编写一些Lambda表达式相关的代码: 接下来想从上面的字符串数组中找到带有"h"的字符串并打印出来: 如果学习了Java8的Lambda表达 ...

  4. selenium安装

    pip安装 pqi优化pip下载速度 pip install selenium pip install --upgrade selenium pip show selenium 离线安装 官网下载se ...

  5. cmds jdbc连接写法

    格式一:  Oracle JDBC Thin using a ServiceName: jdbc:oracle:thin:@//<host>:<port>/<servic ...

  6. python基础语法11 内置模块time,datatime,random,os,sys,hashlib模块

    time模块 在python的三种时间表现形式: 1.时间戳: 给电脑看的. - 自1970-01-01 00:00:00到当前时间,按秒计算,计算了多少秒. 2.格式化时间(Format Strin ...

  7. Docker for Windows 使用命令报错

    could not read CA certificate "C:\Users\username\.docker\machine\machines\default\ca.pem": ...

  8. java.lang.IllegalStateException: Service id not legal hostname (leyou_item_service)

    . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ...

  9. OpenCV 学习笔记(9)RGB转换成灰度图像的一个常用公式Gray = R*0.299 + G*0.587 + B*0.114

    https://blog.csdn.net/fly_wt/article/details/86432886 RGB转换成灰度图像的一个常用公式是:Gray = R*0.299 + G*0.587 + ...

  10. [Algorithm] 46. Permutations

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...