10 examples of 'python convert string to date' in Python

Every line of 'python convert string to date' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
48def convert2date(self, string):
49 try:
50 date = dateutil.parser.parse(string, default=self.default_date)
51 if date.year != 9999:
52 return date
53 except (ValueError, TypeError, OverflowError):
54 pass
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

39def convert_date(date):
40 return datetime.datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.%fZ")
133def translate_string_2_date(date = '1992-02-24'):
134 date_list = date.split('-')
135 data_time = datetime.date(int(date_list[0]), int(date_list[1]), int(date_list[2]))
136 return data_time
254def date_str(s):
255 return datetime.datetime.strptime(s, '%Y-%m-%d').date()
11def to_datetime(date_str: str) -> datetime:
12 d = date_str
13 try:
14 dte = datetime.strptime(d, "%Y-%m")
15 except ValueError:
16 dte = date_str
17 return dte
19def parse_date(date):
20 if not date:
21 return None
22 try:
23 return datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
24 except ValueError:
25 d = datetime.datetime.strptime(date, '%Y-%m-%d').date()
26 return d
28def _parse_date(datestr):
29 return dateutil.parser.parse(datestr)
21def date(value, format='%Y-%m-%d'):
22 '''
23 >>> date('2000-01-01')
24 datetime.date(2000, 1, 1)
25 '''
26 return DateTime.strptime(value, format).date()
82def str_to_dt(date):
83 ''' Converts Yahoo Date Strings to datetime objects. '''
84 yr = int(date[0:4])
85 mo = int(date[5:7])
86 day = int(date[8:10])
87 return datetime.datetime(yr,mo, day)
23def convert_date_format(self, str, format):
24 """
25 Convert date string to other format date string.
26 The converter parser is based on the specifications below.
27
28 https://dateutil.readthedocs.io/en/stable/parser.html
29
30 Args:
31 str: string before convert
32 format: formatter
33
34 Returns:
35 str: new format date string or None if str is None
36 """
37 if not str:
38 return None
39
40 p = parser.parse(str)
41 return p.strftime(format)

Related snippets