본문 바로가기

datetime26

Python 왜 datetime.datetime.utcnow()은 시간대 정보를 포함하지 않을까요?, Why does datetime.datetime.utcnow() not contain timezone information? 질문 datetime.datetime.utcnow() 왜 이 datetime은 명시적으로 UTC datetime임에도 불구하고 시간대 정보가 없는 걸까요? 이게 tzinfo를 포함하고 있을 것으로 예상했는데요. 답변 Python 3.2 이후로는 datetime 모듈에 datetime.timezone이 포함되어 있습니다. datetime.utcnow()의 문서에는 다음과 같이 나와 있습니다: 인식 가능한 현재 UTC 날짜 및 시간은 datetime.now(timezone.utc)를 호출하여 얻을 수 있습니다. 따라서, datetime.utcnow()은 tzinfo를 설정하지 않으며 UTC임을 나타내지 않지만, datetime.now(datetime.timezone.utc)는 tzinfo가 설정된 UTC 시.. 2023. 11. 24.
Python 파이썬에서 현재 시간을 가져오고, 연도, 월, 일, 시간, 분으로 나누는 방법은 다음과 같습니다., How to get current time in python and break up into year, month, day, hour, minute? 질문 파이썬에서 현재 시간을 가져와 year, month, day, hour, minute과 같은 변수에 할당하고 싶습니다. 이를 파이썬 2.7에서 어떻게 할 수 있을까요? 답변 datetime 모듈은 친구입니다: import datetime now = datetime.datetime.now() print(now.year, now.month, now.day, now.hour, now.minute, now.second) # 2015 5 6 8 53 40 별도의 변수가 필요하지 않습니다. 반환된 datetime 객체의 속성에는 필요한 모든 정보가 있습니다. 2023. 11. 14.
Python 파이썬에서 datetime 객체를 epoch 이후의 밀리초로 변환하는 방법은 무엇인가요?, How can I convert a datetime object to milliseconds since epoch (unix time) in Python? 질문 나는 Python datetime 객체를 Unix 시간 또는 1970년 이래로 초/밀리초 단위로 변환하고 싶습니다. 어떻게 해야 합니까? 답변 나에게는 이것을 수행하는 가장 간단한 방법으로 보입니다. import datetime epoch = datetime.datetime.utcfromtimestamp(0) def unix_time_millis(dt): return (dt - epoch).total_seconds() * 1000.0 2023. 11. 2.
Python 파이썬 날짜 문자열을 날짜 객체로 변환하기, Python date string to date object 질문 파이썬에서 문자열을 날짜 객체로 변환하는 방법은 무엇인가요? 문자열은 "24052010" (형식: "%d%m%Y")이 됩니다. datetime.datetime 객체가 아닌 datetime.date 객체를 원합니다. 답변 당신은 Python의 strptime을(를) 사용할 수 있습니다. 이는 datetime 패키지에 있습니다: >>> import datetime >>> datetime.datetime.strptime('24052010', "%d%m%Y").date() datetime.date(2010, 5, 24) 2023. 10. 27.