interget or string to date

280 views
Skip to first unread message

Omi Chiba

unread,
Nov 2, 2011, 11:19:39 AM11/2/11
to web2py-users
This is more like python question but

jj.J2NSD is integer and has value = 20111130 (YYYYMMDD)

I want to set the value as default value of FFNSD which is 'date'
type.

Field('FFNSD', 'date'),

I think I need to import datetime and convert from integer or stiring.
Is anyone know easy way to do it ?


This doesn't work because it set string to date...

db.FEDEXFORM.FFNSD.default = str(jj.J2NSD)[0:4]+"-"+str(jj.J2NSD)
[4:6]+"-"+str(jj.J2NSD)[6:8]

Vineet

unread,
Nov 2, 2011, 11:27:23 AM11/2/11
to web2py-users
Recently, I wrote this function for my purpose.
See if this helps you.
Assuming that argument 'dte' input format is -->> "03-11-2011"

[CODE]
import datetime, string

def str_to_dt(dte):
dte = str(dte)
dte = string.strip(dte)
if len(dte) == 10:
y = int(dte[6:])
m = int(dte[3:5])
d = int(dte[:2])
return datetime.date(y, m, d)
[/CODE]

This returns output in "python datetime.date" format.

--Vineet

Omi Chiba

unread,
Nov 2, 2011, 3:09:35 PM11/2/11
to web2py-users
Vineet,

Thanks for sharing.
It works like a charm !

Phyo Arkar

unread,
Nov 4, 2011, 8:35:56 AM11/4/11
to web...@googlegroups.com
please check dateutils.

It works will almost all date-time formats, it intelligently convert
to datetime object .

labix.org/python-dateutil

Sébastien Loix

unread,
Dec 20, 2014, 2:15:33 AM12/20/14
to web...@googlegroups.com
An old post to solve a 2014 problem. Thanks for sharing! :)

Massimo Di Pierro

unread,
Dec 29, 2014, 5:03:22 AM12/29/14
to web...@googlegroups.com

much simpler. :-)

db.FEDEXFORM.FFNSD.default =  datetime.datetime.strptime(jj.J2NSD,'%Y%m%d')

Reply all
Reply to author
Forward
0 new messages