I've been working a lot with date/time format, especially when facing
the problem of daylight time change...
Here is an example of my code, using time.strftime and time.strptime
#format is EDI 203, meaning CCYYMMDDHHMM > XML format is CCYY-MM-
DDTHH:MMZ
# converting a string ('201112010500') into a date/time format (use
time.strptime(date value, date format)
creationDT=time.strptime(inn.get({'BOTSID':'UNH'},
{'BOTSID':'DTM','C507.2005':'137','C507.2380':None}),'%Y%m%d%H%M')
# putting the date time value into my output file (use time.strftime
to convert the date/time value (creationDT) into a string, according
to the specified format ('%Y-%m-%dT%H:%MZ'))
out.put({'BOTSID':'ConsumptionMeasurement','CreationDateTime':'','CreationDateTime__v':time.strftime('%Y-
%m-%dT%H:%MZ',creationDT)})
Hope this helps.
JF
On 14 déc, 19:35, Akhilesh Kumar <k.akhil...@gmail.com> wrote:
> Hello Henk-Jan,
>
> I'm trying to convert the date format from DD/MM/YYYY to DD/MM/YY (8 chars)
> but not able to find any function to do so.
> All I got from one of the Plugin is this function *
> time.strftime('%Y%m%d%H%M')* but this does not gives any output. It only
in bots 2.1.0 there is a function called 'datemask'
use it like this:
converted_date = transform.datemask(old_date,' DD/MM/CCYY','DD/MM/YY')
is easy to backport (mind the import)
henk-jan
On 12/14/2011 07:35 PM, Akhilesh Kumar wrote:
> Hello Henk-Jan,
>
> I'm trying to convert the date format from DD/MM/YYYY to DD/MM/YY (8 chars) but not able to find any function to do so.
> All I got from one of the Plugin is this function *time.strftime('%Y%m%d%H%M')* but this does not gives any output. It only gives the current Date/Time.
This function is new in version 2.1.0
You can easily add it to earlier versions, just edit transform.py and
add the following code at the bottom. I have included "import
collections" in the function, you can put it at top with other imports
if you wish.
Kind Regards,
Mike
---- code ----
def datemask(value,frommask,tomask):
''' value is formatted according as in frommask;
returned is the value formatted according to tomask.
'''
import collections
if not value:
return value
convdict = collections.defaultdict(list)
for key,value in zip(frommask,value):
convdict[key].append(value)
terug = ''
for c in tomask:
terug += convdict.get(c,[c]).pop(0)
return terug
BHT04DT3 = transform.datemask(BHT04DT,'DD/MM/CCYY','YYYYMMDD')
should be
BHT04DT3 = transform.datemask(BHT04DT,'DD/MM/YYYY','YYYYMMDD')
or
BHT04DT3 = transform.datemask(BHT04DT,'DD/MM/CCYY','CCYYMMDD')
The content of the string does not matter, the datemask function will
just rearrange the characters.
There are a lot of useful ways to manipulate strings in Python. The
links below are good reference points.
http://docs.python.org/tutorial/introduction.html#strings
http://docs.python.org/library/stdtypes.html#string-methods
Kind Regards,
Mike