Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get date format from a date string?

I use dateparser.parse to turn a string date into a datetime object:

>>> dateparser.parse(u'22 Décembre 2010')
datetime.datetime(2010, 12, 22, 0, 0)

But now I want to create new dates with the same string format. How can I get this?

>>> get_date_pattern(u'22 Décembre 2010')
'%d %B %Y'

Edit: I'll clarify that I don't know what the string format is (I'm iterating through a list of many date strings, and for each one I want to create a new date in the same format). I'm not looking to convert a datetime object to string, I'm looking to take a formatted string and determine what that format is.

like image 849
user1956609 Avatar asked Nov 29 '15 18:11

user1956609


People also ask

How do you parse a date?

parse() The Date. parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31). Only the ISO 8601 format ( YYYY-MM-DDTHH:mm:ss.

How do I format a string to date in Python?

Use datetime. strftime(format) to convert a datetime object into a string as per the corresponding format . The format codes are standard directives for mentioning in which format you want to represent datetime. For example, the %d-%m-%Y %H:%M:%S codes convert date to dd-mm-yyyy hh:mm:ss format.

Which function is used convert string into date format?

Date() Function. as. Date() function in R Language is used to convert a string into date format.

How do you format a date and time string?

A standard date and time format string uses a single character as the format specifier to define the text representation of a DateTime or a DateTimeOffset value. Any date and time format string that contains more than one character, including white space, is interpreted as a custom date and time format string .

How to convert Java String to date using simpledateformat?

As you can see from that example, the steps to convert from a given Java String to Date using SimpleDateFormat are: If the SimpleDateFormat.parse method can't parse the given String, it will throw a ParseException If the SimpleDateFormat.parse method can parse the given String, it converts the String to a Date object

What is date parsing and date formatting?

Here, the strDate string is in the format “dd/MM/yyyy” which is needed to be parsed to create a new Date object with the exact components (day, month and year). This is referred as date parsing. One also needs to display a Date object as a String in a specified date pattern. This is referred as date formatting.

How do I parse a date from a string in Java?

Attempt to parse a String that is supposed to contain a date in the format we expect. If the SimpleDateFormat.parse method can't parse the given Java String, it will throw a ParseException. If the SimpleDateFormat.parse method can parse the given String, it converts the Java String to a Date object.


2 Answers

You can use a 3rd party lib dateutil.

from dateutil import parser
dt = parser.parse("06 April, 2019")

To install this, you can do:

pip install python-dateutil
like image 148
Akash Suryawanshi Avatar answered Oct 27 '22 06:10

Akash Suryawanshi


From the datetime documentation:

datetime.strftime(format)

Return a string representing the date and time, controlled by an explicit format string. For a complete list of formatting directives, see section strftime() and strptime() Behavior.

like image 44
ennuikiller Avatar answered Oct 27 '22 04:10

ennuikiller