Sounds simple but....trying to convert multiple date entries (mm/dd/yyyy) in a date field to a string field with yyyymmdd format. I know it can be done with tbx but need a simple line of vbscript (not python) for this action. Is this possible? Lots of string to date solutions but not so much the other way. Thanks for the ideas.
for a python solution should someone need it... consider
n = a datetime.datetime object (ie datetime.now() in any format
"{}{:02.0f}{:02.0f}".format(n.year, n.month, n.day)
'20170322'
# or
"{}-{:02.0f}-{:02.0f}".format(n.year, n.month, n.day) # with separators
'2017-03-22'
You can use:
Year( [DateField] ) & "/" & Month( [DateField] ) & "/" & Day( [DateField] )
or
Year( [DateField] ) & Month( [DateField] ) & Day( [DateField] )
perhaps if you want a formatted string with leading zeros ... use the show codeblock option. In any event the above vbscript code (and mine as well) will fail if NULL Date values are found and your will have to handle your logic for missing dates!
CodeBlock:
strDate = CDate([BirthDate])
strDay = DatePart("d", strDate)
strMonth = DatePart("m", strDate)
strYear = DatePart("yyyy", strDate)
If strDay < 10 Then
strDay = "0" & strDay
End If
If strMonth < 10 Then
strMonth = "0" & strMonth
End If
FormattedDate = strYear & "/" & strMonth & "/" & strDay
Bottom Window:
FormattedDate
sample output "2017/03/05"
Ted... more fodder for numpy is nullness
>>> from datetime import datetime as dt
>>> n = dt.now()
>>> n
datetime.datetime(2017, 3, 23, 13, 48, 21, 718178) # a standard datetime object
>>> import numpy as np # now lets look at numpy datetime implementation
>>>
>>> # ---- introducing --- Not a Number (NaN) and ... Not a Time (NaT) ......
>>> not_a_time = np.datetime64('NaT') # hang on... NaN has a twin in time NaT
>>> not_a_time
numpy.datetime64('NaT')
>>>
>>> # do some type conversions to check for 'none-ness'
>>>
>>> not_a_time.astype('int8') == 0
True
>>>
>>> # now let the good times roll by converting out standard datetime to np.datetime
>>>
>>> good_time = np.datetime64(n)
numpy.datetime64('2017-03-23T13:48:21.718178')
>>>
>>> # type conversion again
>>> good_time.astype('int8') == 0
False
>>>
>>> # you will always have a good time, when numpy is around...
ahhhh .... the different ways of treating nothingness! Who would of thought the concept of nothing was this difficult. I only brought up Null time values because Sean asked specifically for a vbscript and the time functions will fail if a null date is encountered....
Nothing is Something! Any thoughts on incorporating the code block (keeping leading zeros) into an "IIF" statement that could be used as an expression (only VB) in the 'Attribute Assistant'? I just need to suck that date with zeros into another field. Not too sure of the syntax that would be needed.
Thanks
CodeBlock:
strDate = CDate([BirthDate])
strDay = DatePart("d", strDate)
strMonth = DatePart("m", strDate)
strYear = DatePart("yyyy", strDate)
If strDay < 10 Then
strDay = "0" & strDay
End If
If strMonth < 10 Then
strMonth = "0" & strMonth
End If
FormattedDate = strYear & "/" & strMonth & "/" & strDay
Bottom Window:
FormattedDate
I assume that python doesn't work in your environment? as per my example from late yesterday
No, I'm working with an Add-In called Attribute Assistant which is VB centric (possible python support in the future). Thanks, though, useful examples are always welcome.
I have no experience or familiarity with Attribute Assistant, however if you want it embedded within a IIF statement, MS access if the closest I have that contains the IIF function....
Putting this into a single line IIF format is ugly .... (ReformatDateString is a value field)
DatePart("yyyy",CDate([inspdate])) & "/" & IIf(DatePart("m",CDate([inspdate]))<10,"0" & DatePart("m",CDate([inspdate])) & "/",DatePart("m",CDate([inspdate])) & "/") & IIf(DatePart("d",CDate([inspdate]))<10,"0" & DatePart("d",CDate([inspdate])),DatePart("d",CDate([inspdate])))