zoukankan      html  css  js  c++  java
  • time

    def parse_time(self, date):
        if re.match('刚刚', date):
            date = time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time()))
        if re.match('d+分钟前', date):
            minute = re.match('(d+)', date).group(1)
            date = time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time() - float(minute) * 60))
        if re.match('d+小时前', date):
            hour = re.match('(d+)', date).group(1)
            date = time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time() - float(hour) * 60 * 60))
        if re.match('昨天.*', date):
            date = re.match('昨天(.*)', date).group(1).strip()
            date = time.strftime('%Y-%m-%d', time.localtime() - 24 * 60 * 60) + ' ' + date
        if re.match('d{2}-d{2}', date):
            date = time.strftime('%Y-', time.localtime()) + date + ' 00:00'
        return date
    import time;  # 引入time模块
    
    # 当前时间戳
    ticks = time.time()
    print "当前时间戳为:", ticks         #  1459994552.51
    
    # 获取当前时间
    localtime = time.localtime(time.time())
    print "本地时间为 :", localtime
    import time
    
    # 格式化成2016-03-20 11:45:39形式
    print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 
    
    # 格式化成Sat Mar 28 22:24:24 2016形式
    print time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()) 
      
    # 将格式字符串转换为时间戳
    a = "Sat Mar 28 22:24:24 2016"
    print time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))
    
    # 2016-04-07 10:25:09        #  常用
    # Thu Apr 07 10:25:09 2016
    # 1459175064.0
    import datetime
    import json
     
    dic = {
        'k1':123,
        'ctime':datetime.datetime.now()
    }
     
    class MyEncoder(json.JSONEncoder):
        def default(self, o): # o是数据类型
            if isinstance(o,datetime.datetime):
                return o.strftime('%Y-%m-%d')
            else:
                return super(MyEncoder,self).default(o)
     
    v = json.dumps(dic,cls=MyEncoder)
    print(v)
    # {"k1": 123, "ctime": "2018-05-15"}
    View Code
  • 相关阅读:
    React.render和reactDom.render的区别
    CSS中position的4种定位详解
    React.js入门必须知道的那些事
    JS处理事件小技巧
    React.js深入学习详细解析
    React.js实现原生js拖拽效果及思考
    Linux ./configure && make && make install 编译安装和卸载
    Redis set集合结构及命令详解
    Redis数据过期策略
    Redis TTL命令
  • 原文地址:https://www.cnblogs.com/nick477931661/p/9164197.html
Copyright © 2011-2022 走看看