python输入时间的格式,在python的字典中插入日期格式为dd-mm-yyyy的值

I am creating a dictionary having some values including a date of birth of a person. But when I run my code, it is giving an error "datetime.datetime has no attribute datetime" . Here is my code:

import re

from datetime import date

import datetime

from datetime import datetime

userDetails=[]

dateOfBith='9-9-1992'

#datetimeparse = datetime.strptime(dateOfBith,'%dd-%mm-%yyyy')

datetimeparse = datetime.datetime.strptime(dateOfBith, '%Y-%m-%d').strftime('%d/%m/%y')

dateparse = datetime(datetimeparse)

accountDetails= {"FirstName": "ajay", "LastName": "kumar","Account Number":"4242342345234","Balance Currency":"Rs","Balance Amount":"5000"}

accountDetails["DateOfBirth"] = dateparse

解决方案

Do your dates always look the same? If not:

Installation

pip3 install python-dateutil

pip3 install pytz

Code

import datetime

import dateutil

import dateutil.parser

import pytz

date_in = dateOfBith = '9-9-1992'

try:

if date_in.count('.') >= 2:

date_out = dateutil.parser.parse(date_in, dayfirst=True)

else:

date_out = dateutil.parser.parse(date_in, dayfirst=False)

# Additional parameters for parse:

# dayfirst=True # In ambiguous dates it is assumed that the day is further forward.

# default=datetime.datetime(1,1,1,0,0,0) # Used only piecewise, i.e. if the year is missing, year will be 1, the rest remains!

# fuzzy=True # Ignores words before and after the date.

except ValueError as e:

print(repr(e))

print(date_out.isoformat()) # returns 1992-09-09T00:00:00

timezone_utc = pytz.utc

timezone_berlin = pytz.timezone('Europe/Berlin') # Put your capital here!

try:

date_out = timezone_berlin.localize(date_out)

except ValueError as e:

print(repr(e))

print(date_out.isoformat()) # returns 1992-09-09T00:00:00+02:00

if date_out > timezone_utc.localize(datetime.datetime.utcnow()):

print('The date is in the future.')

Tested with Python 3.4.3 on Ubuntu 14.04.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值