Using MS Date Styles for Python

I began scripting with Python to automate nightly routines. After time many of my scripts would include similar variables such as date/time formatting. 

For log files I usually apply this format:
Sample output: RoutineLog_20150622.log
Date Format: yyyymmdd

For an email body I usually apply this format:
Sample: "The routine succeeded June 22, 2015 02:10PM."
Date Format: Mon dd yyyy hh:mm AM/PM

Although I tend to apply similar formatting, I noticed different naming conventions as more and more scripts were written.

Python Script A would call it datetime while script B called it ShortDate.

dateTime1 = time.strftime("%Y%m%d") #yyyymmdd
dateTime2 = time.strftime ("%b %d %Y %I:%M%p")

I like standards and remembered that Microsoft (MS) has datetime style codes. This is what I now reference when setting date time formatting variables.

https://msdn.microsoft.com/en-us/library/ms187928.aspx

Although, MS does not account for each and every style one could use, it will suite my needs.

First part of the variable distinguishes between date, date/time, or time only. The second part of the variable uses an MS Date/Time Style Code (if applicable).

Examples:


 date101 = time.strftime("%m/%d/%Y")  
 date112 = time.strftime("%Y%m%d")  
 date1 = time.strftime("%m/%d/%y")   
 datetime100 = time.strftime ("%b %d %Y %I:%M%p")  
 datetime120 = time.strftime("%Y%m%d_%H%M%S")  
 time108 = time.strftime("%H:%M:%S")  

Comments

Popular posts from this blog

Updating GDB_Items XML Definitions

Animation with ArcGIS Pro

Add Field to a SQL Geodatabase using T-SQL