IT/Dart

Dart - DateTime / Duration 사용법 정리

쿠와아앙 2022. 11. 21. 23:39
반응형

 

Dart 에서 

날짜를 다룰때 어떤식으로 정리해야될지 확인해보겠습니다

 

void main() {
  DateTime now = DateTime.now();
  
  print(now);
  print(now.year);
  print(now.month);
  print(now.day);
  print(now.hour);
  print(now.minute);
  print(now.second);
  print(now.millisecond);
  
  
  Duration duration = Duration (seconds: 60);
  
  print(duration);
  print(duration.inDays);
  print(duration.inHours);
  print(duration.inMinutes);
  print(duration.inSeconds);
  print(duration.inMilliseconds);
  
  DateTime specificDays = DateTime(
    2017,
    11,
    23,
  );
  
  print(specificDays);
  
  final difference = now.difference(specificDays);
  
  print(difference);
  print(difference.inDays);
  print(difference.inHours);
  print(difference.inMinutes);
  
  print(now.isAfter(specificDays));
  print(now.isBefore(specificDays));
  
  print(now);
  print(now.add(Duration(hours:10)));
  print(now.subtract(Duration(seconds:20)));
  
}

 

 


DateTime 

 

class DateTime implements Comparable<DateTime>

An instant in time, such as July 20, 1969, 8:18pm GMT.

DateTimes can represent time values that are at a distance of at most 100,000,000 days from epoch (1970-01-01 UTC): -271821-04-20 to 275760-09-13.

Create a DateTime object by using one of the constructors or by parsing a correctly formatted string, which complies with a subset of ISO 8601. Note: hours are specified between 0 and 23, as in a 24-hour clock.

For example:

final now = DateTime.now();
final berlinWallFell = DateTime.utc(1989, 11, 9);
final moonLanding = DateTime.parse('1969-07-20 20:18:04Z'); // 8:18pm

A DateTime object is anchored either in the UTC time zone or in the local time zone of the current computer when the object is created.

Once created, neither the value nor the time zone of a DateTime object may be changed.

You can use properties to get the individual units of a DateTime object.

print(berlinWallFell.year); // 1989
print(berlinWallFell.month); // 11
print(berlinWallFell.day); // 9
print(moonLanding.hour); // 20
print(moonLanding.minute); // 18

For convenience and readability, the DateTime class provides a constant for each day and month name - for example, august and friday. You can use these constants to improve code readability:

final berlinWallFell = DateTime.utc(1989, DateTime.november, 9);
print(DateTime.november); // 11
assert(berlinWallFell.month == DateTime.november);
assert(berlinWallFell.weekday == DateTime.thursday);

Day and month values begin at 1, and the week starts on Monday. That is, the constants january and monday are both 1.

Working with UTC and local time

A DateTime object is in the local time zone unless explicitly created in the UTC time zone. Use isUtc to determine whether a DateTime object is based in UTC.

final dDay = DateTime.utc(1944, 6, 6);
print(dDay.isUtc); // true

final dDayLocal = DateTime(1944, 6, 6);
print(dDayLocal.isUtc); // false

Use the methods toLocal and toUtc to get the equivalent date/time value specified in the other time zone.

final localDay = dDay.toLocal(); // e.g. 1944-06-06 02:00:00.000
print(localDay.isUtc); // false

final utcFromLocal = localDay.toUtc(); // 1944-06-06 00:00:00.000Z
print(utcFromLocal.isUtc); // true

Use timeZoneName to get an abbreviated name of the time zone for the DateTime object.

print(dDay.timeZoneName); // UTC
print(localDay.timeZoneName); // e.g. EET

To find the difference between UTC and the time zone of a DateTime object call timeZoneOffset.

print(dDay.timeZoneOffset); // 0:00:00.000000
print(localDay.timeZoneOffset); // e.g. 2:00:00.000000

Comparing DateTime objects

The DateTime class contains methods for comparing DateTimes chronologically, such as isAfter, isBefore, and isAtSameMomentAs.

print(berlinWallFell.isAfter(moonLanding)); // true
print(berlinWallFell.isBefore(moonLanding)); // false
print(dDay.isAtSameMomentAs(localDay)); // true

Using DateTime with Duration

Use the add and subtract methods with a Duration object to create a DateTime object based on another. For example, to find the point in time that is 36 hours after now, you can write:

final now = DateTime.now();
final later = now.add(const Duration(hours: 36));

To find out how much time is between two DateTime objects use difference, which returns a Duration object:

final difference = berlinWallFell.difference(moonLanding);
print(difference.inDays); // 7416

The difference between two dates in different time zones is just the number of nanoseconds between the two points in time. It doesn't take calendar days into account. That means that the difference between two midnights in local time may be less than 24 hours times the number of days between them, if there is a daylight saving change in between. If the difference above is calculated using Australian local time, the difference is 7415 days and 23 hours, which is only 7415 whole days as reported by inDays.

Other resources

  • See Duration to represent a span of time.
  • See Stopwatch to measure timespans.
  • The DateTime class does not provide internationalization. To internationalize your code, use the intl package.

Open library docs 

 

 


 

Duration

class Duration implements Comparable<Duration>

A span of time, such as 27 days, 4 hours, 12 minutes, and 3 seconds.

A Duration represents a difference from one point in time to another. The duration may be "negative" if the difference is from a later time to an earlier.

Durations are context independent. For example, a duration of 2 days is always 48 hours, even when it is added to a DateTime just when the time zone is about to make a daylight-savings switch. (See DateTime.add).

Despite the same name, a Duration object does not implement "Durations" as specified by ISO 8601. In particular, a duration object does not keep track of the individually provided members (such as "days" or "hours"), but only uses these arguments to compute the length of the corresponding time interval.

To create a new Duration object, use this class's single constructor giving the appropriate arguments:

const fastestMarathon = Duration(hours: 2, minutes: 3, seconds: 2);

The Duration represents a single number of microseconds, which is the sum of all the individual arguments to the constructor.

Properties can access that single number in different ways. For example the inMinutes gives the number of whole minutes in the total duration, which includes the minutes that were provided as "hours" to the constructor, and can be larger than 59.

const fastestMarathon = Duration(hours: 2, minutes: 3, seconds: 2);
print(fastestMarathon.inDays); // 0
print(fastestMarathon.inHours); // 2
print(fastestMarathon.inMinutes); // 123
print(fastestMarathon.inSeconds); // 7382
print(fastestMarathon.inMilliseconds); // 7382000

The duration can be negative, in which case all the properties derived from the duration are also non-positive.

const overDayAgo = Duration(days: -1, hours: -10);
print(overDayAgo.inDays); // -1
print(overDayAgo.inHours); // -34
print(overDayAgo.inMinutes); // -2040

Use one of the properties, such as inDays, to retrieve the integer value of the Duration in the specified time unit. Note that the returned value is rounded down. For example,

const aLongWeekend = Duration(hours: 88);
print(aLongWeekend.inDays); // 3

This class provides a collection of arithmetic and comparison operators, plus a set of constants useful for converting time units.

const firstHalf = Duration(minutes: 45); // 00:45:00.000000
const secondHalf = Duration(minutes: 45); // 00:45:00.000000
const overTime = Duration(minutes: 30); // 00:30:00.000000
final maxGameTime = firstHalf + secondHalf + overTime;
print(maxGameTime.inMinutes); // 120

// The duration of the firstHalf and secondHalf is the same, returns 0.
var result = firstHalf.compareTo(secondHalf);
print(result); // 0

// Duration of overTime is shorter than firstHalf, returns < 0.
result = overTime.compareTo(firstHalf);
print(result); // < 0

// Duration of secondHalf is longer than overTime, returns > 0.
result = secondHalf.compareTo(overTime);
print(result); // > 0

See also:

  • DateTime to represent a point in time.
  • Stopwatch to measure time-spans.

Open library docs 

 

 

반응형