10 examples of 'datetime format python' in Python

Every line of 'datetime format 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
43def fmtdatetime(dt):
44 hour = datetime.strftime(dt, "%I").lstrip('0')
45 return datetime.strftime(dt, "%m/%d {0}%p".format(hour))
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

28def format_datetime(value, format='medium'):
29 if format == 'full':
30 format="d. MMMM y 'at' HH:mm"
31 elif format == 'medium':
32 format="dd.MM.y HH:mm"
33 return dates.format_datetime(value, format)
17def format_datetime(dt):
18 """Return Open-States-style datetime string from datetime object."""
19 return dt.strftime(DATETIME_TEMPLATE)
64def format_datetime(dt: datetime) -> str:
65 """
66 Format a datetime object as string.
67
68 :param dt: Date and time to format.
69 :return: String representation.
70 """
71 return dt.strftime("%Y-%m-%d %H:%M:%S%z")
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
47def format_datetime(datestr, formatting):
48 parsed_dt = dateutil.parser.parse(datestr)
49 return parsed_dt.strftime(formatting)
43def FormatDate(date, format=None):
44 ms = int(date[6:-7])
45 date = datetime.datetime.fromtimestamp(ms/1000.0)
46
47 if format:
48 return datetime.datetime.strftime(date, format)
49 else:
50 return date
47def format_datetime_long(
48 dt: datetime, *, seconds: bool = False, smart: bool = True
49) -> str:
50 date_string = format_date_long(dt.date(), smart=smart)
51 time_string = format_time(dt.time(), seconds=seconds)
52
53 return f'{date_string}, {time_string}'
658def test_format_datetime():
659 dt = datetime(2007, 4, 1, 15, 30)
660 assert (dates.format_datetime(dt, locale='en_US') ==
661 u'Apr 1, 2007, 3:30:00 PM')
662
663 full = dates.format_datetime(dt, 'full', tzinfo=timezone('Europe/Paris'),
664 locale='fr_FR')
665 assert full == (u'dimanche 1 avril 2007 à 17:30:00 heure '
666 u'd\u2019\xe9t\xe9 d\u2019Europe centrale')
667 custom = dates.format_datetime(dt, "yyyy.MM.dd G 'at' HH:mm:ss zzz",
668 tzinfo=timezone('US/Eastern'), locale='en')
669 assert custom == u'2007.04.01 AD at 11:30:00 EDT'
95def format_dt(dt: Union[datetime, date]):
96 if 4 <= dt.day <= 20 or 24 <= dt.day <= 30:
97 suffix = 'th'
98 else:
99 suffix = ['st', 'nd', 'rd'][dt.day % 10 - 1]
100 fmt = datetime_fmt if isinstance(dt, datetime) else date_fmt
101 # ampm is an ugly fix for missing locales on travis
102 fmt_ = fmt.format(suffix=suffix, day=dt.day, ampm=f'{dt:%p}'.lower())
103 return dt.strftime(fmt_)

Related snippets