728x90

datetime 3

[Python] Can't compare naive and aware 에러 발생 시

Python 에서 datetime.now() 로 현재 날짜시간을 확인하면 시간이 잘 나온다. 그런데 이렇게 생성한 값으로 다른 datetime 값이랑 비교하거나 연산할 때 naive 하다면서 에러가 날 때가 있다. 이렇게 에러가 나오는 이유는 이 datatime 값이 timezone을 모르는 navie 한 값이기 때문이다. (timezone을 몰라도 일단 쓰게 해줌) 해결을 위해서는 아래와 같이 이미 생성한 datetime.now() 값에 timezone을 설정하면 된다. from pytz import timezone ... if ( a_date_time_variable > datetime.now().replace(tzinfo=timezone('Asia/Seoul')): print("a_date_time..

IT 2023.01.26

[Flutter 앱 개발하기] DateTime 변수 format 변경하기

DateTime 변수의 포맷을 변환 시키려면 DateFormat을 사용하면 된다. import 'package:intl/intl.dart'; final DateTime now = DateTime.now(); final DateFormat formatter = DateFormat('yyyy-MM-dd'); final String formatted = formatter.format(now); print(formatted); 보다 다양한 포맷 옵션을 찾아보려면, DateFormat class - intl library - Dart API DateFormat class Null safety DateFormat is for formatting and parsing dates in a locale-sensitive..

IT 2022.12.30

[Flutter 앱 개발하기] 두 개의 DateTime 변수 차이 구하기

두 Datetime 변수 간의 날짜 차이를 구하고 싶다면 아래와 같이 하면 된다. DateTime now = DateTime.now(); DateTime dt = someTimestampValue.toDate(); if(10 > now.difference(dt).inDays) { print("Difference is less than 10."); } else { print("Difference is more than or equal to 10."); } 현재 시간 구할 때는 now() 사용하기. ​ Timestamp 값을 DateTime 값으로 변환할 때는 toDate() 사용하기. ​ 여기서는 사용 안했지만, 문자열로 DateTime 생성할 때는 아래와 같이 parse() 사용하기. DateTime d..

IT 2022.12.30
728x90