10 examples of 'python string to date' in Python

Every line of 'python 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
254def date_str(s):
255 return datetime.datetime.strptime(s, '%Y-%m-%d').date()
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

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()
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)
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)
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
17def string_to_date(datestring):
18 return datetime.strptime(datestring, "%Y-%m-%d %H:%M:%S.%f")
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
62def deserialize_date(string: str) -> datetime.date:
63 """Deserializes string to date.
64
65 :param string: str.
66 :return: date.
67 """
68 try:
69 from dateutil.parser import parse
70 return parse(string).date()
71 except ImportError:
72 return string

Related snippets