Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Shezan Kazi
Shezan Kazi
10,807 Points

Python 3 - timestamp from today morning 00:00:00

Hi,

how would I get the timestamp for today morning midnight? I can't seem to get this done..

Thanks!

Ken Alger
Ken Alger
Treehouse Teacher

Shezan;

I'm not sure I understand your question. What are you attempting to get the display to show?

Ken

Shezan Kazi
Shezan Kazi
10,807 Points

Hi Ken,

I use this code to get today's date and time for midnight and end of day

import datetime


# defining global variables
today = datetime.datetime.now()
today_morning = today.strftime('%Y%m%d') + "000000"
today_evening = today.strftime('%Y%m%d') + "235959"

What I need is to display a unix timestamp for these times e.g.: 20161019000000 --> 1476835200

2 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Shezan;

You could use the timestamp() method.

import datetime

dt = datetime.datetime.now().timestamp()

Will assign an Unix timestamp value to dt. You could also do something like:

import datetime

dt = datetime.datetime.now().strftime('%s')

Is that what you are wanting? You might also want to take into consideration time zones, depending on your use case.

Post back with further questions and happy coding,
Ken

Shezan Kazi
Shezan Kazi
10,807 Points

Hi Ken, I did try those, so thanks for your tips. However timestamp only delivers the timestamp for this very second. I, however, want the timestamp for start and beginning of the day, i.e. 00:00:00 and 23:59:59 o'clock.

That's where I'm stuck.