파이썬[Python]: datetime - datetime 클래스

datetime 모듈 - datetime 클래스(class)


/// 설명

날짜와 시간을 가지고 있는 단일 객체입니다.(year, month, day 인자는 요구되어집니다.) tzinfo는 tzinfo의 서브클래스이고, fold는 일광 절약 시간제(Daylight saving time, DST)와 관련 있는 것으로 0 또는 1 을 사용합니다.

※ 형식
class datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)

reference
https://docs.python.org/3/library/datetime.html#module-datetime

/// 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import datetime
 
year = datetime.datetime.utcnow().year
month = datetime.datetime.utcnow().month
day = datetime.datetime.utcnow().day
hour = datetime.datetime.utcnow().hour
minute = datetime.datetime.utcnow().minute
second = datetime.datetime.utcnow().second
microsecond = datetime.datetime.utcnow().microsecond
 
test_dt = datetime.datetime(year=year, month=month, day=day, hour=hour, minute=minute,
                            second=second, microsecond=microsecond, tzinfo=None)
 
print(test_dt)  # 2021-11-24 05:46:35.339074
 
test_timezone = datetime.timezone(datetime.timedelta(0), 'UTC')
 
test_dt = datetime.datetime(year=year, month=month, day=day, hour=hour, minute=minute,
                            second=second, microsecond=microsecond, tzinfo=test_timezone)
 
print(test_dt)  # 2021-11-24 05:46:35.339074+00:00
 
# Korean Standard Time: KST = UTC + 9
test_timezone = datetime.timezone(datetime.timedelta(hours=9), 'KST')
 
test_dt = datetime.datetime(year=year, month=month, day=day, hour=hour, minute=minute,
                            second=second, microsecond=microsecond, tzinfo=test_timezone)
 
print(test_dt)  # 2021-11-24 05:46:35.339074+09:00
print(test_timezone.fromutc(test_dt))  # 2021-11-24 14:46:35.339074+09:00
 
cs

* 실행환경: Microsoft Windows 10 Homes
* 인터프리터: 파이썬(Python 3.9)


– 당신을 응원합니다. –

댓글

이 블로그의 인기 게시물

파이썬[Python]: 내장함수 - from_bytes 메서드

파이썬[Python]: 내장함수 - __len__ 메서드

파이썬[Python]: kivy - 한글 사용

파이썬[Python]: 내장함수 - bit_length 메서드

C 언어: sin 함수, cos 함수, tan 함수