Dart DateTime second
Syntax & Examples
Syntax of DateTime.second
The syntax of DateTime.second property is:
int second This second property of DateTime the second [0...59].
Return Type
DateTime.second returns value of type int.
✐ Examples
1 Get the second of the current DateTime
In this example,
- We create a DateTime object
dtusing theDateTime.now()constructor, which represents the current date and time. - We then access the
secondproperty ofdtto get the second. - We print the result to standard output.
Dart Program
void main() {
var dt = DateTime.now();
print(dt.second);
}Output
36 (Output varies depending on the current date and time.)
2 Get the second of a specific DateTime in UTC
In this example,
- We create a DateTime object
dtusing theDateTime.utc()constructor, specifying the year, month, day, hour, minute, and second. - We then access the
secondproperty ofdtto get the second. - We print the result to standard output.
Dart Program
void main() {
var dt = DateTime.utc(2024, 5, 3, 10, 15, 30);
print(dt.second);
}Output
30
3 Get the second of a parsed DateTime
In this example,
- We create a DateTime object
dtby parsing a date-time string '2024-05-03T10:15:30' using theDateTime.parse()method. - We then access the
secondproperty ofdtto get the second. - We print the result to standard output.
Dart Program
void main() {
var dt = DateTime.parse('2024-05-03T10:15:37');
print(dt.second);
}Output
37
Summary
In this Dart tutorial, we learned about second property of DateTime: the syntax and few working examples with output and detailed explanation for each example.