10 examples of 'how to change date format in python' in Python

Every line of 'how to change date format in 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
this disclaimer
17def format_datetime(str_datetime, old_format, new_format):
18 """字符串时间格式更改
19 2013-10-10 23:40:00 --> 2013/10/10 23:40:00
20 """
21 time_array = time.strptime(str_datetime, old_format)
22 new_format = time.strftime(new_format, time_array)
23 return new_format
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

395def format_date(d, dateformat="%Y-%m-%d"):
396 """
397 Formats a python date to the format given (strftime rules)
398 """
399 if d is None: return ""
400 try:
401 return time.strftime(dateformat, d.timetuple())
402 except:
403 return ""
62def date_handler(obj):
63 return obj.isoformat() if hasattr(obj, 'isoformat') else obj
40def format_date(date):
41 """Returns a formatted date string in a CloudTrail date format"""
42 return date.strftime(DATE_FORMAT)
24def _date_handler(obj):
25 if isinstance(obj, datetime.datetime):
26 return local_time_to_online(obj)
27 elif isinstance(obj, (featureservice.FeatureService,
28 layer.FeatureLayer,
29 layer.TableLayer)):
30 return dict(obj)
31 else:
32 return obj
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)
531def _convert_to_datetime(self, date, input_format):
532 if isinstance(date, datetime):
533 return date
534 if is_number(date):
535 return self._seconds_to_datetime(date)
536 if is_string(date):
537 return self._string_to_datetime(date, input_format)
538 raise ValueError("Unsupported input '%s'." % date)
6def format_date(date_object):
7 """Formats the date and returns the datetime object"""
8 # date_time = date_object.split("+")
9 return datetime.strptime(str(date_object), "%Y-%m-%dT%H:%M:%S")
15def test_http_date_python(self):
16 format_date_time(time())
245def get_friendly_date(date: datetime.datetime) -> str:
246 """
247 Returns a string form of a date/datetime.
248 """
249 if date is None:
250 return ""
251 try:
252 return date.strftime("%d %B %Y") # e.g. 03 December 2013
253 except Exception as e:
254 raise type(e)(str(e) + f' [value was {date!r}]')

Related snippets