Python - Date/Time simple example

Here is a simple example using date/time functionality in Python.


#!/usr/bin/python
import datetime


def main():
    print "starting the main function..."
    now = datetime.datetime.now()
    print now.year, now.month, now.day, now.hour, now.minute, now.second, now.microsecond
    file_name = str(now.year) + "_" + str(now.month).zfill(2) + "_" + str(now.hour).zfill(2) + "_" + str(now.minute).zfill(2) + "_" + str(now.second).zfill(2) + "_" + str(now.microsecond) + ".json"
    print "file_name is:" + file_name

    # Another way using tuples
    now2 = datetime.datetime.now()
    time = now2.timetuple()
    print time.tm_year, time.tm_mon, time.tm_mday, time.tm_hour, time.tm_min, time.tm_sec


if __name__ == "__main__": main()


- almirsCorner.com -

#python #programming #code #coding #software #softwaredeveloper #softwareengineering #programmer



No comments:

Post a Comment