본문 바로가기
파이썬

[파이썬 강좌] 11. 날짜함수 (DateTime)

by GDBS 2020. 8. 28.
반응형
728x170

이전 강좌에서 Python OOP 개념 에 대해 자세히 설명했습니다.

우리는 Python에서 지원하는 상속, 오버로딩, 재정의 및 데이터 숨김과 같은 여러 가지 oops 기둥과 함께 클래스, 객체, 생성자 및 oops에 대해 자세히 배웠습니다.

 

이 강좌에서는 Python DateTime이라는 추가 Python 개념에 대해 설명합니다. Python 개념을 자세히 이해하려면 Python 강좌 목록을 읽어보십시오.

 

 

Python DateTime

Python에서 날짜, 시간 및 DateTime은 DateTime을 처리하는 여러 내장 함수를 제공하는 내장 클래스입니다.

이 함수는 현재 날짜, 시간 및 요일을 가져 오는 데 사용됩니다.

위의 모든 항목에 대한 몇 가지 예를 살펴 보겠습니다.

예 1 :

from datetime import date

 

def test_date():

       today = date.today()

       print(“Today’s date is”, today)

 

test_date()

산출:

오늘 날짜는 2018-09-29입니다.

산출:

예 2 :

from datetime import date

 

def test_date():

       today = date.today()

      #To print individual date componets

       print(“Date components are:”, today.day, today.month, today.year)

 

test_date()

산출:

날짜 구성 요소 : 2018 년 9 월 29 일

산출:

예 3 :

from datetime import date

 

def test_date():

       today = date.today()

      #To print the weekday number(0=Monday , 6=Sunday)

      print(“Weekday number is:”, today.weekday())

 

test_date()

산출:

평일 번호 : 5

산출:

예 4 :

from datetime import datetime

 

def test_date():

       today = datetime.now()

       #Print the curren date and time

       print(“Current date and time is:”, today)

 

test_date()

산출:

현재 날짜 및 시간 : 2018-09-29 21 : 26 : 09.578260

산출:

예 5 :

from datetime import datetime

 

def test_date():

       time = datetime.time(datetime.now())

      #to retrieve the current time

      print(“Current time is:”, time)

 

test_date()

산출:

현재 시간 : 21 : 28 : 32.980759

산출:

strftime () 메서드를 사용하여 날짜 및 시간 서식 지정

예 6 :

import datetime

print(“Current date and time is:”, datetime.datetime.now())

print(“Current date and time using strftime method:”, datetime.datetime.now().strftime(“%y-%m-%d-%H-%M”)

print(“Current year is:”, datetime.date.today().strftime(“%Y”))

print(“Month of year is:”, datetime.date.today().strftime(“%B”))

print(“Week number of the year is:”, datetime.date.today().strftime(“%W”))

print(“Weekday of the week is:”, datetime.date.today().strftime(“%w”))

print(“Day of the year is:”, datetime.date.today().strftime(“%j”))

print(“Day of the month is:”, datetime.date.today().strftime(“%d”))

print(“Day of the week is:”, datetime.date.today().strftime(“%A”))

출력 :

현재 날짜 및 시간 : 2018-09-29 21 : 32 : 30.643372
strftime 메서드를 사용하는 현재 날짜 및 시간 : 18-09-29-21-32
현재 연도 : 2018 년
월 : 9 월
주 번호 년 : 39
요일 : 6
요일 : 272
일 : 29
요일 : 토요일

산출:

 

 

이 튜토리얼에서는 문자열 내장 함수의 추가 주제와 파일 열기 및 삭제와 같은 기타 개념과 DateTime에서 제공하는 내장 메소드에 대해 자세히 설명했습니다.

다가오는 튜토리얼에서는 Python String 함수에 대해 자세히 설명합니다 !!

 

 

 

728x90
반응형

댓글