10 examples of 'remove time from datetime python' in Python

Every line of 'remove time from datetime python' 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
40def set_time_string_on_datetime(dt, time_string):
41 """
42 Given a datetime DT and a string in the form HH:MM return a
43 new datetime with the hour and minute set according to
44 TIME_STRING
45 """
46 hour, minute = stringy_time_to_inty_time(time_string)
47 dt = dt.replace(hour=hour, minute=minute)
48 return dt
14def remove_tz_helper(datetime_object):
15 return datetime_object.replace(tzinfo=None)
37def time_to_datetime(t):
38 return _combine_date_time(date.today(), t)
81def parse_datetime(dt: str) -> datetime:
82 return datetime.fromisoformat(dt[:-4])
5def parse_datetime(datetime_str):
6 if datetime_str:
7 return datetime.strptime(datetime_str, '%Y-%m-%d %H:%M')
31def get_time(raw_time):
32 return raw_time.replace('T', ' ')\
33 .replace('+', ' +')\
34 .replace('08:00', '0800')
117def get_time(data=None):
118 """Receive a dictionary or a string and return a datatime instance.
119
120 data = {"year": 2006,
121 "month": 11,
122 "day": 21,
123 "hour": 16,
124 "minute": 30 ,
125 "second": 00}
126
127 or
128
129 data = "21/11/06 16:30:00"
130
131 2018-04-17T17:13:50Z
132
133 Args:
134 data (str, dict): python dict or string to be converted to datetime
135
136 Returns:
137 datetime: datetime instance.
138
139 """
140 if isinstance(data, str):
141 date = datetime.strptime(data, "%Y-%m-%dT%H:%M:%S")
142 elif isinstance(data, dict):
143 date = datetime(**data)
144 else:
145 return None
146 return date.replace(tzinfo=timezone.utc)
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)
69def parse_datetime_to_utc(time_str, manual_format=None):
70 """
71 Attempt to parse the string time_str with the given string format.
72 If no format is given, attempt to automatically parse the given string
73 that may or may not contain timezone information.
74 Returns a datetime object of the string in UTC
75 or None if the parsing was unsuccessful.
76 """
77 if manual_format and isinstance(manual_format, basestring):
78 timeobj = datetime.strptime(time_str, manual_format)
79 else: # automatic parsing
80 if len(time_str) > 26 and time_str[26] in ['+', '-']:
81 try:
82 timeobj = datetime.strptime(time_str[:26],'%Y-%m-%dT%H:%M:%S.%f')
83 except ValueError:
84 return None
85 if time_str[26]=='+':
86 timeobj -= timedelta(hours=int(time_str[27:29]), minutes=int(time_str[30:]))
87 elif time_str[26]=='-':
88 timeobj += timedelta(hours=int(time_str[27:29]), minutes=int(time_str[30:]))
89 elif len(time_str) == 26 and '+' not in time_str[-6:] and '-' not in time_str[-6:]:
90 # nothing known about tz, just parse it without tz in this cause
91 try:
92 timeobj = datetime.strptime(time_str[0:26],'%Y-%m-%dT%H:%M:%S.%f')
93 except ValueError:
94 return None
95 else:
96 # last try: attempt without milliseconds
97 try:
98 timeobj = datetime.strptime(time_str, "%Y-%m-%dT%H:%M:%S")
99 except ValueError:
100 return None
101 return timeobj.replace(tzinfo=tz.tzutc())
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])

Related snippets