상세 컨텐츠

본문 제목

Datetime In Dart

Dart

by techbard 2024. 1. 16. 08:18

본문

반응형
void main(List<String> args) {
  // How To Get Date And Time
  print(DateTime.now());
  print("");

  // Get Year, Month, Day Of Datetime In Dart
  DateTime dt = DateTime.now();
  print("year: ${dt.year}");
  print("month: ${dt.month}");
  print("day: ${dt.day}");
  print("hour: ${dt.hour}");
  print("minutes: ${dt.minute}");
  print("second: ${dt.second}");
  print("");

  // How To Convert String To DateTime
  String myDateInString = "2022-02-22";
  DateTime myConvertedData = DateTime.parse(myDateInString);
  print("year: ${myConvertedData.year}");
  print("month: ${myConvertedData.month}");
  print("day: ${myConvertedData.day}");
  print("");

  // Example Add Date In Dart
  DateTime myTargetDay = DateTime.parse("2020-01-10");
  myTargetDay = myTargetDay.add(Duration(days: 1));
  print("Add 1day's year: ${myTargetDay.year}");
  print("Add 1day's month: ${myTargetDay.month}");
  print("Add 1day's day: ${myTargetDay.day}");
  print("");

  // Find Difference Between Two Dates In Dart
  DateTime somePreviousDay = DateTime.parse("2000-03-03");
  DateTime today = DateTime.now();
  Duration diff = today.difference(somePreviousDay);
  print("Diff in days: ${diff.inDays}");
  print("Diff in hours: ${diff.inHours}");
  print("Diff in minutes: ${diff.inMinutes}");
  print("Diff in seconds: ${diff.inSeconds}");
  print("Diff in milliseconds: ${diff.inMilliseconds}");
  print("Diff in microseconds: ${diff.inMicroseconds}");
}

/*

Note: Final variables will have a value known at runtime. 
Const variables have a value known at compile time. 
Instance variable can be final but not const.

*/

// 결과
2024-01-16 08:16:34.923308

year: 2024
month: 1
day: 16
hour: 8
minutes: 16
second: 34
year: 2022
month: 2
day: 22
Add 1day's year: 2020
Add 1day's month: 1
Add 1day's day: 11
Diff in days: 8719
Diff in hours: 209264
Diff in minutes: 12555856
Diff in seconds: 753351394
Diff in milliseconds: 753351394931
Diff in microseconds: 753351394931314

 

void main(List<String> args) {
  // Multiline Single-Quoted String
  String multilineSingleQuotedString = '''
  This is a multiline
  single-quited string.
''';

  print(multilineSingleQuotedString);

  // Multiline Double-Quoted String
  String multilineDoubleQuotedString = """
  This is a multiline
  double-quoted string.
""";

  print(multilineDoubleQuotedString);
}

// 결과
  This is a multiline
  single-quited string.

  This is a multiline
  double-quoted string.
반응형

관련글 더보기

댓글 영역