美文网首页
Python 时间类 获取年月周 开始时间

Python 时间类 获取年月周 开始时间

作者: 霡霂976447044 | 来源:发表于2020-11-10 14:08 被阅读0次
    import datetime
    from datetime import timedelta
    
    
    def get_day_str():
        return datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d")
    
    
    def local2utc(local_dtm):
        # 本地时间转 UTC 时间( -8:00 )
        return datetime.datetime.utcfromtimestamp(local_dtm.timestamp())
    
    
    def get_week_start():
        """获取本周的开始时间"""
        now = datetime.datetime.now()
        now = datetime.datetime.combine(now, datetime.datetime.min.time())
        this_week_start = now - timedelta(days=now.weekday())
        return this_week_start
    
    
    def get_today():
        """获取今天的时间"""
        now = datetime.date.today()
        dt = datetime.datetime.combine(now, datetime.datetime.min.time())
        return dt
    
    
    def get_month_start():
        """获取这个月的开始时间"""
        now = datetime.datetime.now()
        this_month_start = datetime.datetime(now.year, now.month, 1)
        return this_month_start
    
    
    def get_year_start():
        now = datetime.datetime.now()
        now = datetime.datetime(year=now.year, month=1, day=1)
        return now
    

    相关文章

      网友评论

          本文标题:Python 时间类 获取年月周 开始时间

          本文链接:https://www.haomeiwen.com/subject/sshjbktx.html