#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Datetime.now Output Format Issue


    Just registered and a little about me I am a first year student in programming and I am getting a little head start on my school work by learning some new things on my own. I thought that I would give Python a chance to try learning on my own and I have come to a bit of a hill I can not get over...

    What I have..

    Code:
    from datetime import datetime
    now = datetime.now()
    print(now)
    gives

    Code:
    2013-02-05 21:01:02.732361
    Now what I have been trying to get out of this is to use the now variable and use + in conjunction with a str() to print() the output in a mm/dd/yy form basically i can achieve all three on the same line but are only seperated by whitespace. I know this seems simple but I am really starting from nothing but some good math skills and alot of problem solving skills. I have googled for what seems like hours and have not been able to find anything i could use. It is looking for the most simple way by concatenating two strings... Plz and thank you ahead of time.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    Btwvthis is for code academy.com lol not a class take 5 mins of life to check if ya don't believe me but seriously any help would be nice maybe not even an answer but some advice.

    I know I can pull now.month
    now.day
    now.year

    But its forming the str() I am missing..
    I can pull each part of mm/dd/yyyy and output is ie: 2 5 2013

    But what I can't seem to get is 2/5/2013
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    This is the easy way:
    Code:
    >>> import time
    >>> time.strftime('%D')
    '02/05/13'
    This is worse:
    Code:
    # you could convert to integer then back to string to remove leading zeros.
    (DATE,TIME,) = '2013-02-05 21:01:02.732361'.split()
    (YYYY,MM,DD,) = DATE.split('-')
    AMBIGUOUS_AMERICAN_FORM = '/'.join((DD,MM,YYYY[-2:],))
    print(AMBIGUOUS_AMERICAN_FORM)
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    I have tried it wont take strtime()

    Like I said before they want this as simple as possible using the existing now variable. How about I rephrase my question if I had

    print(x, y, z)

    if would output x y z
    now how about adding "/" in between each variable?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    439
    Rep Power
    67
    Originally Posted by qwiklilhonda
    I have tried it wont take strtime()
    Try the correct spelling for a while

    How about I rephrase my question if I had

    print(x, y, z)

    if would output x y z
    now how about adding "/" in between each variable?
    Code:
    print('{0}/{1}/{2}'.format(x, y, z))
    print('/'.join([str(spam) for spam in (x, y, z)]))
    My armada: openSUSE 13.2 (home desktop, work desktop), openSUSE 13.1 (home laptop), Debian GNU/Linux 7.7.0 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    [QUOTE=SuperOscar]Try the correct spelling for a while

    My apologies I was half asleep when getting ready for work when I posted it but I had it write in my coding.. again using what I have is giving the correct output but it still will not take it as a correct answer. They are really looking for this to be super simple and want to emphasize using "+" or concatenating the variables.

    I have come to the conclusion that in assigning the variables ie:

    x = now.month

    Is it possible to add the "/" in here as opposed to in the print() function.



    Sorry for all the beginner questions but I feel as if this will be part of my make it break it style of learning if I cant grasp these basic concepts then future projects may be out of the question.....
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    str(1)+'/'+str(3)+'/'
    Who is the "they" and how do "they" define simple? I can direct you to many months worth of "simple" debates in a forum I follow. (I've stayed out of the discussion, but definitely side with those who appreciate the entire language rather than with those who try to use bizarre structures to avoid language elements they dislike.)
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    Currently at work so I will attempt the problem when I do get home and again I apologize "They" are codeacademy.com and "simple "may just be bad wording on my part. If your curious visit the site and you want to go to python... And it is section 2... Underneath is a project called time and date.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    Code:
    from datetime import datetime
    now = datetime.now()
    print(now)
    
    x = now.month
    y = now.day 
    z = now.year
    
    print(str(x) + '/' + str(y) + '/' + str(z))
    Thats what codeacdemy was looking for. (plant palm in face now). I am new to all of this and I am making beginner mistakes.
  18. #10
  19. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65
    Like SuperOscar pointed out, you can create a properly formatted date string this way ...
    Code:
    # newer Python formatting
    date_str = "{}/{}/{}".format(x,y,z)
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  20. #11
  21. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    Originally Posted by http://linux.die.net/man/3/strftime
    %D

    Equivalent to %m/%d/%y. (Yecch-for Americans only. Americans should note that in other countries %d/%m/%y is rather common. This means that in international context this format is ambiguous and should not be used.) (SU)
    I use DEC format
    YYYY-MM-DD:HH:MM:SS

    Comments on this post

    • Dietrich agrees : best for sorting dates
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo