【问题标题】:How to print current date on python3?如何在python3上打印当前日期?
【发布时间】:2013-06-07 20:00:13
【问题描述】:

据我所知,here(例如),这应该以两位数打印当前年份

print (datetime.strftime("%y"))

但是,我得到了这个错误

TypeError: descriptor 'strftime' requires a 'datetime.date' object but received a 'str'

所以我尝试了这个

print (datetime.strftime(datetime.date()))

得到

TypeError: descriptor 'date' of 'datetime.datetime' object needs an argument

所以我把"%y" 放在上面的thr date() 里面得到

TypeError: descriptor 'date' requires a 'datetime.datetime' object but received a 'str'

对我来说,这开始显得非常古怪和可疑。

我错过了什么?

【问题讨论】:

    标签: python date datetime time


    【解决方案1】:

    此功能允许您以多种格式获取日期和时间(请参阅本文底部)。

    # Get the current date or time
    def getdatetime(timedateformat='complete'):
        from datetime import datetime
        timedateformat = timedateformat.lower()
        if timedateformat == 'day':
            return ((str(datetime.now())).split(' ')[0]).split('-')[2]
        elif timedateformat == 'month':
            return ((str(datetime.now())).split(' ')[0]).split('-')[1]
        elif timedateformat == 'year':
            return ((str(datetime.now())).split(' ')[0]).split('-')[0]
        elif timedateformat == 'hour':
            return (((str(datetime.now())).split(' ')[1]).split('.')[0]).split(':')[0]
        elif timedateformat == 'minute':
            return (((str(datetime.now())).split(' ')[1]).split('.')[0]).split(':')[1]
        elif timedateformat == 'second':
            return (((str(datetime.now())).split(' ')[1]).split('.')[0]).split(':')[2]
        elif timedateformat == 'millisecond':
            return (str(datetime.now())).split('.')[1]
        elif timedateformat == 'yearmonthday':
            return (str(datetime.now())).split(' ')[0]
        elif timedateformat == 'daymonthyear':
            return ((str(datetime.now())).split(' ')[0]).split('-')[2] + '-' + ((str(datetime.now())).split(' ')[0]).split('-')[1] + '-' + ((str(datetime.now())).split(' ')[0]).split('-')[0]
        elif timedateformat == 'hourminutesecond':
            return ((str(datetime.now())).split(' ')[1]).split('.')[0]
        elif timedateformat == 'secondminutehour':
            return (((str(datetime.now())).split(' ')[1]).split('.')[0]).split(':')[2] + ':' + (((str(datetime.now())).split(' ')[1]).split('.')[0]).split(':')[1] + ':' + (((str(datetime.now())).split(' ')[1]).split('.')[0]).split(':')[0]
        elif timedateformat == 'complete':
            return str(datetime.now())
        elif timedateformat == 'datetime':
            return (str(datetime.now())).split('.')[0]
        elif timedateformat == 'timedate':
            return ((str(datetime.now())).split('.')[0]).split(' ')[1] + ' ' + ((str(datetime.now())).split('.')[0]).split(' ')[0]
    

    要获取时间或日期,只需使用getdatetime("<TYPE>"),将<TYPE> 替换为以下参数之一:

    所有示例输出都使用此模型信息:25-11-2017 03:23:56.477017

    Argument Meaning Example output
    day Get the current day 25
    month Get the current month 11
    year Get the current year 2017
    hour Get the current hour 03
    minute Get the current minute 23
    second Get the current second 56
    millisecond Get the current millisecond 477017
    yearmonthday Get the year, month and day 2017-11-25
    daymonthyear Get the day, month and year 25-11-2017
    hourminutesecond Get the hour, minute and second 03:23:56
    secondminutehour Get the second, minute and hour 56:23:03
    complete Get the complete date and time 2017-11-25 03:23:56.477017
    datetime Get the date and time 2017-11-25 03:23:56
    timedate Get the time and date 03:23:56 2017-11-25

    【讨论】:

    • 一个非常方便的功能。我是否可以建议您将代码中的“参数/含义/示例输出”信息作为 cmets 包含在内,以便可以将函数和注释都复制为一个块?
    【解决方案2】:

    我每次都用这个是标准的

    import datetime
    now = datetime.datetime.now()
    print ("Current date and time : ")
    print (now.strftime("%Y-%m-%d %H:%M:%S"))
    

    【讨论】:

      【解决方案3】:

      我总是使用这个代码,它在一个元组中打印年份到第二个

      import datetime
      
      now = datetime.datetime.now()
      
      time_now = (now.year, now.month, now.day, now.hour, now.minute, now.second)
      
      print(time_now)
      

      【讨论】:

        【解决方案4】:
        import datetime
        now = datetime.datetime.now()
        
        print(now.year)
        

        上面的代码对我来说非常好。

        【讨论】:

          【解决方案5】:

          以下似乎有效:

          import datetime
          print (datetime.datetime.now().strftime("%y"))
          

          它想要的 datetime.data 对象位于点的“左侧”而不是右侧。您需要一个日期时间的实例来调用该方法,您可以通过 now()

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2023-02-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-10-07
            • 1970-01-01
            相关资源
            最近更新 更多