10 examples of 'convert str to datetime' in Python

Every line of 'convert str to datetime' 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
8def convert_to_datetime(datetime_str):
9 tweet_datetime = datetime.strptime(datetime_str,'%a %b %d %H:%M:%S %z %Y')
10
11 return(tweet_datetime)
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

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
39def str_to_datetime(str):
40 """
41 Converts a string to a datetime object using OpenERP's
42 datetime string format (exemple: '2011-12-01 15:12:35').
43
44 No timezone information is added, the datetime is a naive instance, but
45 according to OpenERP 6.1 specification the timezone is always UTC.
46 """
47 if not str:
48 return str
49 return datetime.datetime.strptime(str.split(".")[0], DEFAULT_SERVER_DATETIME_FORMAT)
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)
5def parse_datetime(datetime_str):
6 if datetime_str:
7 return datetime.strptime(datetime_str, '%Y-%m-%d %H:%M')
43def convert_datetime(s):
44 """
45 Convert a datetime string to a standard format
46 """
47 try:
48 dt = DateTimeFromString(s)
49 date_string = dt.Format("%m/%d/%Y %H:%M:%S")
50 return date_string
51 except:
52 return s
39def convert_date(date):
40 return datetime.datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.%fZ")
50def to_datetime(timestring):
51 """Convert one of the bitbucket API's timestamps to a datetime object."""
52 format = '%Y-%m-%d %H:%M:%S'
53 return datetime.datetime(*time.strptime(timestring, format)[:7])
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)
26def to_datetime(string):
27 """Load UTC time as a string into a datetime object."""
28 try:
29 # Format is 2011-07-05 01:26:12.084316
30 return datetime.datetime.strptime(
31 string.split('.', 1)[0], '%Y-%m-%d %H:%M:%S')
32 except ValueError:
33 return datetime.datetime.strptime(string, '%Y-%m-%d')

Related snippets