Python date and time


#1



Please help, I've been stuck on this for quite a while and can't figure out what I've done wrong.

https://www.codecademy.com/courses/python-beginner-en-zFPOx/0/5?curriculum_id=4f89dab3d788890003000096

Oops, try again. Your printed date and time do not seem to be in the right format: mm/dd/yyyy hh:mm:ss


from datetime import datetime
now = datetime.now()

print '%s/%s/%s' % (now.month , now.day , now.year) 
print '%s:%s:%s' % (now.hour ,now.minute , now.second)


#2

well I did it like this and it worked:
print '%s/%s/%s %s:%s:%s' % (now.month, now.day, now.year, now.hour, now.minute, now.second)
I guess the problem with your code is that you print date separately from time, they should be next to each other.:slight_smile:


#3

this is how it worked for me. You have to be careful with the spacing of the single quote ' it will not work

from datetime import datetime
now = datetime.now()

print '%s/%s/%s %s:%s:%s' % (now.month, now.day, now.year, now.hour, now.minute, now.second)


#5

Dear programmers even though your code works it is not correct.

print '%s/%s/%s' % (now.month, now.day, now.year)

As a programmer you'll must follow the procedures. In this question we are only asked to print the date in form of mont/day/year. Nobody asked for time. For most of the people code doesn't work because they think only about the results. We are asked to:

Print the current date in the form of mm/dd/yyyy.

as well as:

Change the string so that it uses a / character in between the %s placeholders instead of a - character.

Author of this thread forgot to format the line of code (interpreter is not a human he's job is to check the first line which is wrong). Your suggestions are almost correct, interpreter saw the "true" or in this case "True" part of your code even though it's completely wrong. You gave twice as much output as needed. In real world programing it is crucial to do what they ask you to do. If i could give you any advice it would be: Clean your mind before coding and focus on what you doing". There is big difference between making "working" code and good code. For good code you need full understanding of what you are doing, for working code you need nothing more than digital typewriter.


#6

date and time has printed but still i'm getting error message like this(Your printed date and time do not seem to be in the right format: mm/dd/yyyy hh:mm:ss)

from datetime import datetime
now = datetime.now()

print '%s:%s:%s %s/%s/%s' % ( now.month, now.day, now.year, now.hour, now.minute, now.second)


#7

can anyone help me with this error?


#8

Have you read my answer? You need to print month/day/year. You give to much output. And you forgot to replace signs in between placeholders.

print "%s/%s/%s" (now.month, now.day, now.year)

what you tried to do was printing mm:dd:yyyy instead of mm/dd/yyyy (it is one of international date formats included in ISO ":" are used only for time purposes hh:mm:ss)

If your code doesn't work you should read question again, then look at your code and ask yourself if your logic is correct according to the question. Then you should look for syntax mistakes.


#9

Thank you so much. I solved this issue.


#10

This here helped me get past the problem.


#11

thanks my program also worked with your solution


#12

A Python program can handle date and time in several ways. Converting between date formats is a common chore for computers. Python's time and calendar modules help track dates and times.

Time intervals are floating-point numbers in units of seconds. Particular instants in time are expressed in seconds since 12:00am, January 1, 1970(epoch).

There is a popular time module available in Python which provides functions for working with times, and for converting between representations. The function time.time() returns the current system time in ticks since 12:00am, January 1, 1970(epoch).
Learn Python by visiting here.

#!/usr/bin/python
import time;  # This is required to include time module.


ticks = time.time()
print "Number of ticks since 12:00am, January 1, 1970:", ticks

This would produce a result something as follows −

Number of ticks since 12:00am, January 1, 1970: 7186862.73399
Date arithmetic is easy to do with ticks. However, dates before the epoch cannot be represented in this form. Dates in the far future also cannot be represented this way - the cutoff point is sometime in 2038 for UNIX and Windows.


#13

The line of code has to look like this:

print '%s/%s/%s %s:%s:%s' % ( now.month, now.day, now.year, now.hour, now.minute, now.second)

The mistake you are making is the slash and the colons have to be in different spots. Since time and Date "look" different.

You welcome


#14

Please help! Don't know what I did wrong....

from datetime import datetime
now = datetime.now()

print '05/23/2017' % (now.month, now.day, now.year)