Profile

Click to view full profile
Hi, I'm Veerapat Sriarunrungrueang, an expert in technology field, especially full stack web development and performance testing.This is my coding diary. I usually develop and keep code snippets or some tricks, and update to this diary when I have time. Nowadays, I've been giving counsel to many well-known firms in Thailand.
view more...

Wednesday, December 1, 2010

How to get current Date/Time in Python

In my project, sometimes I have to used current date to compare. So, we need to get date from the system.
import datetime
dt = datetime.datetime.now()
print dt.year
print dt.month
print dt.day
print dt.hour
print dt.minute
print dt.second
Very easy right ^^

4 comments:

  1. Also strftime is very handy:
    datetime.datetime.today().strftime("%Y-%m-%d %H:%M")
    Out[25]: '2011-09-05 03:57'

    ReplyDelete
  2. Oh, thanks a lot @marwal. I have never used string format in Python yet. And Can we adjust any format that we want to? or it's need to be standard time format?

    ReplyDelete
  3. If I understand your question, yes.. you could format the parts of time and parts of date into whatever you'd need.

    Maybe you can tell me something: Are teachers (Linux, Apache, PHP, Python, SQL) needed in Thailand or Laos? I would very much want to live and work in those countries.

    ReplyDelete
  4. In Thailand, yes, they are needed for Linux, Apache, and SQL. Most of computer science courses in Thailand do not include Python (usually Java).

    ReplyDelete