如何在python3上打印当前日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17053099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:18:14  来源:igfitidea点击:

How to print current date on python3?

pythondatedatetimetime

提问by Yotam

From what I gather, here(for example), this should print the current year in two digits

根据我收集的信息,这里(例如),这应该以两位数打印当前年份

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

However, I get this error

但是,我收到此错误

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

So I tried to this

所以我试着这样做

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

to get

要得到

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

so I placed "%y"inside of thr date()above to get

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

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

Which start to seem really loopy and fishy to me.

对我来说,这开始看起来真的很疯狂和可疑。

What am I missing?

我错过了什么?

采纳答案by Yotam

import datetime
now = datetime.datetime.now()

print(now.year)

The above code works perfectly fine for me.

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

回答by Paul Rubel

The following seems to work:

以下似乎有效:

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

The datetime.data object that it wants is on the "left" of the dot rather than the right. You need an instance of the datetime to call the method on, which you get through now()

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

回答by Richie Bendall

This Code Will Allow You To Get The Date And Time In Any Format You Can Think Of: (Look At The Bottom Of This Post For More Information)

此代码将允许您以您能想到的任何格式获取日期和时间:(有关更多信息,请查看本文底部)

# 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]

To Get The Time Or Date, Just Type getdatetime('TYPE')And Replace TYPEWith AnyOf The Following (Remember: Ensure That When You Type It, You Surround It With Speech Marks (") Or Apostraphies (') )

为了获取时间或日期,只需键入getdatetime('TYPE')和替换TYPE任何以下(记住:确保当您键入它,您应将其用言语引号(“)或者Apostraphies('))

All Example Outputs Based Around The Same Time And Date:

Date: 25-11-2017 | Time: 03:23:56.477017

基于相同时间和日期的所有示例输出:

日期:25-11-2017 | 时间:03:23:56.477017

Table Of Arguments With Meanings And Example Outputs

带有含义和示例输出的参数表

回答by Amir_N

I always use this code, which print the year to second in a tuple

我总是使用此代码,它在元组中打印年份到秒

import datetime

now = datetime.datetime.now()

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

print(time_now)

回答by Antu

I use this which is standard for every time

我每次都使用这个标准

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

回答by Rajesh Kumar

Please try the below lines in order to get the current date.

请尝试以下几行以获得当前日期。

import datetime

now = datetime.datetime.now()

print (now.strftime("%Y-%m-%d %H:%M:%S"))

Output:
2020-02-26 21:15:32

Refer : https://beginnersbug.com/how-to-get-the-current-date-in-pyspark-with-example/

参考:https: //beginnersbug.com/how-to-get-the-current-date-in-pyspark-with-example/