Insert a value in date format dd-mm-yyyy in dictionary in python

2024/11/24 10:35:48

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
Answer

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 pytzdate_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:00timezone_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:00if 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.

https://en.xdnf.cn/q/120548.html

Related Q&A

Collisions arent registering with Python Turtles

Im creating a simple 2D shooter following an online tutorial, and the enemy sprites (except 1, there are 5 total) are not abiding by my collision code. Everything works except the bullet object that th…

Function should clean data to half the size, instead it enlarges it by an order of magnitude

This has been driving me nuts all week weekend. I am trying merge data for different assets around a common timestamp. Each assets data is a value in dictionary. The data of interest is stored in lists…

is it possible to add colors to python output? [duplicate]

This question already has answers here:How do I print colored text to the terminal?(66 answers)Closed 10 years ago.so i made a small password strength tester for me, my friends and my family, as seen …

Tkinter Function attached to Button executed immediately [duplicate]

This question already has answers here:How can I pass arguments to Tkinter buttons callback command?(2 answers)Closed 8 years ago.What I need is to attach a function to a button that is called with a …

Error!!! cant concatenate the tuple to non float

stack = []closed = []currNode = problem.getStartState()stack.append(currNode)while (len(stack) != 0):node = stack.pop()if problem.isGoalState(node):print "true"closed.append(node)else:child =…

Using R to fit data from a csv with a gamma function?

Using the following data: time_stamp,secs,count 2013-04-30 23:58:55,1367366335,32 2013-04-30 23:58:56,1367366336,281 2013-04-30 23:58:57,1367366337,664 2013-04-30 23:58:58,1367366338,1255 2013-04-30…

regex multiple string match in a python list

I have a list of strings like this:["ra", "dec", "ra-error", "dec-error", "glat", "glon", "flux", "l", "b"]I ne…

python IndentationError: expected an indented block [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

how to tell an infinite loop to end once one number repeats twice in a row (in python 3.4)

The title says it all. I have an infinite loop of randomly generated numbers from one to six that I need to end when 6 occurs twice in a row.

Is it possible to customize the random function to avoid too much repetition of words? [duplicate]

This question already exists:Customizing the random function without using append, or list, or other container?Closed last year.Theoretically, when randomization is used, the same word may be printed …