parsing - DateTime.TryParse() in Python? -



parsing - DateTime.TryParse() in Python? -

is there equivalent c#'s datetime.tryparse() in python?

edit:

i'm referring fact avoids throwing exception, not fact guesses format.

if don't want exception, grab exception.

try: d = datetime.datetime.strptime(s, "%y-%m-%d %h:%m:%s") except valueerror: d = none

in zen of python, explicit improve implicit. strptime always returns datetime parsed in exact format specified. makes sense, because have define behavior in case of failure, maybe want is.

except valueerror: d = datetime.datetime.now()

or

except valueerror: d = datetime.datetime.fromtimestamp(0)

or

except valueerror: raise webframework.servererror(404, "invalid date")

by making explicit, it's clear next person reads failover behavior is, , need be.

or maybe you're confident date cannot invalid, it's coming database datetime, column, in case there wont' exception catch, , don't grab it.

python parsing datetime tryparse

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -