How to print a date in a regular format?

后端 未结 22 2650
孤街浪徒
孤街浪徒 2020-11-21 23:00

This is my code:

import datetime
today = datetime.date.today()
print(today)

This prints: 2008-11-22 which is exactly what I wa

相关标签:
22条回答
  • 2020-11-21 23:28

    Since the print today returns what you want this means that the today object's __str__ function returns the string you are looking for.

    So you can do mylist.append(today.__str__()) as well.

    0 讨论(0)
  • 2020-11-21 23:31

    I hate the idea of importing too many modules for convenience. I would rather work with available module which in this case is datetime rather than calling a new module time.

    >>> a = datetime.datetime(2015, 04, 01, 11, 23, 22)
    >>> a.strftime('%Y-%m-%d %H:%M')
    '2015-04-01 11:23'
    
    0 讨论(0)
  • 2020-11-21 23:33

    A quick disclaimer for my answer - I've only been learning Python for about 2 weeks, so I am by no means an expert; therefore, my explanation may not be the best and I may use incorrect terminology. Anyway, here it goes.

    I noticed in your code that when you declared your variable today = datetime.date.today() you chose to name your variable with the name of a built-in function.

    When your next line of code mylist.append(today) appended your list, it appended the entire string datetime.date.today(), which you had previously set as the value of your today variable, rather than just appending today().

    A simple solution, albeit maybe not one most coders would use when working with the datetime module, is to change the name of your variable.

    Here's what I tried:

    import datetime
    mylist = []
    present = datetime.date.today()
    mylist.append(present)
    print present
    

    and it prints yyyy-mm-dd.

    0 讨论(0)
  • 2020-11-21 23:36

    The date, datetime, and time objects all support a strftime(format) method, to create a string representing the time under the control of an explicit format string.

    Here is a list of the format codes with their directive and meaning.

        %a  Locale’s abbreviated weekday name.
        %A  Locale’s full weekday name.      
        %b  Locale’s abbreviated month name.     
        %B  Locale’s full month name.
        %c  Locale’s appropriate date and time representation.   
        %d  Day of the month as a decimal number [01,31].    
        %f  Microsecond as a decimal number [0,999999], zero-padded on the left
        %H  Hour (24-hour clock) as a decimal number [00,23].    
        %I  Hour (12-hour clock) as a decimal number [01,12].    
        %j  Day of the year as a decimal number [001,366].   
        %m  Month as a decimal number [01,12].   
        %M  Minute as a decimal number [00,59].      
        %p  Locale’s equivalent of either AM or PM.
        %S  Second as a decimal number [00,61].
        %U  Week number of the year (Sunday as the first day of the week)
        %w  Weekday as a decimal number [0(Sunday),6].   
        %W  Week number of the year (Monday as the first day of the week)
        %x  Locale’s appropriate date representation.    
        %X  Locale’s appropriate time representation.    
        %y  Year without century as a decimal number [00,99].    
        %Y  Year with century as a decimal number.   
        %z  UTC offset in the form +HHMM or -HHMM.
        %Z  Time zone name (empty string if the object is naive).    
        %%  A literal '%' character.
    

    This is what we can do with the datetime and time modules in Python

        import time
        import datetime
    
        print "Time in seconds since the epoch: %s" %time.time()
        print "Current date and time: ", datetime.datetime.now()
        print "Or like this: ", datetime.datetime.now().strftime("%y-%m-%d-%H-%M")
    
    
        print "Current year: ", datetime.date.today().strftime("%Y")
        print "Month of year: ", datetime.date.today().strftime("%B")
        print "Week number of the year: ", datetime.date.today().strftime("%W")
        print "Weekday of the week: ", datetime.date.today().strftime("%w")
        print "Day of year: ", datetime.date.today().strftime("%j")
        print "Day of the month : ", datetime.date.today().strftime("%d")
        print "Day of week: ", datetime.date.today().strftime("%A")
    

    That will print out something like this:

        Time in seconds since the epoch:    1349271346.46
        Current date and time:              2012-10-03 15:35:46.461491
        Or like this:                       12-10-03-15-35
        Current year:                       2012
        Month of year:                      October
        Week number of the year:            40
        Weekday of the week:                3
        Day of year:                        277
        Day of the month :                  03
        Day of week:                        Wednesday
    
    0 讨论(0)
  • 2020-11-21 23:36
    import datetime
    import time
    
    months = ["Unknown","January","Febuary","Marchh","April","May","June","July","August","September","October","November","December"]
    datetimeWrite = (time.strftime("%d-%m-%Y "))
    date = time.strftime("%d")
    month= time.strftime("%m")
    choices = {'01': 'Jan', '02':'Feb','03':'Mar','04':'Apr','05':'May','06': 'Jun','07':'Jul','08':'Aug','09':'Sep','10':'Oct','11':'Nov','12':'Dec'}
    result = choices.get(month, 'default')
    year = time.strftime("%Y")
    Date = date+"-"+result+"-"+year
    print Date
    

    In this way you can get Date formatted like this example: 22-Jun-2017

    0 讨论(0)
  • 2020-11-21 23:37

    You need to convert the date time object to a string.

    The following code worked for me:

    import datetime
    collection = []
    dateTimeString = str(datetime.date.today())
    collection.append(dateTimeString)
    print collection
    

    Let me know if you need any more help.

    0 讨论(0)
提交回复
热议问题