Get YYYYMMDD String from Date

5131
2
Jump to solution
12-22-2013 01:14 AM
JonathanMulder
New Contributor III
I have a script where the user enters a StartDate and EndDate (via arcpy.GetParameterAsText dialog boxes).

I want to return strings of those dates in a "YYYYMMDD" format.  In another script, I used a snippet to loop through the dates and return a string (CurrentDate) in YYYYMMDD format, but I really don't understand how that function works.

Thanks for any help,

Jon Mulder

import arcpy  from datetime import date from dateutil.rrule import rrule, DAILY import time  StartDate = arcpy.GetParameterAsText(0) EndDate = arcpy.GetParameterAsText(0) print StartDate strStartDate = "YYYYMMDD"          #How do I get this format?   for dt in rrule(DAILY, dtstart=DateStart, until=DateEnd):     CurrentDate = dt.strftime("%Y%m%d")  print "Done."
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
Hi Jon,

Not sure how the user is entering the start and end date, but if they enter it as '12/23/2013', you could use the dateutil module to convert this to YYYYMMDD.  Ex:

import dateutil from dateutil import parser  startDate = '12/23/2013'  print parser.parse(startDate).strftime('%Y%m%d')

View solution in original post

0 Kudos
2 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Jon,

Not sure how the user is entering the start and end date, but if they enter it as '12/23/2013', you could use the dateutil module to convert this to YYYYMMDD.  Ex:

import dateutil from dateutil import parser  startDate = '12/23/2013'  print parser.parse(startDate).strftime('%Y%m%d')
0 Kudos
JonathanMulder
New Contributor III
Thanks very much for showing me the parser utility!  I incorporated it into my test code below and it works!

import arcpy
import os

from datetime import date
from dateutil.rrule import rrule, DAILY
import time
from dateutil import parser

DateStart = arcpy.GetParameterAsText(0)
arcpy.AddMessage(DateStart)
strDateStart = parser.parse(DateStart).strftime('%Y%m%d')
arcpy.AddMessage(strDateStart)


Jon Mulder
California Department of Water Resources
0 Kudos