THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
This example shows how to get the Current Date and time in the Dart or Flutter programming language.
DateTime.now()
function.DateFormat.Hms().format()
methodDateFormat.yMd().format(dateTime);
function.Dart provides the DateTime
class to process Date and Time-related functions.
The now()
method returns the current date and time.
Here is an example to get the Current Date and time
main() {
DateTime dateTime = DateTime.now();
print(dateTime.toString()); //2022-03-04 12:35:38.997
int currentYear = dateTime.year;
print(currentYear.toString()); //2022
}
Output:
//2022-03-04 12:35:38.997
//2022
This example returns only time in hours, minutes, and seconds and the format is hh:mm:ss
intl/intl.dart
package into the programHere is an example
import 'package:intl/intl.dart';
main() {
DateTime dateTime = DateTime.now();
String timeFormat = DateFormat.Hms().format(dateTime);
print(timeFormat); //12:42:49
}
This example gets the current date in the MM/DD/YYYY
format only.
intl/intl.dart
package into the program using the import keywordDateTime
object with the now() function.yMd()
function where yMd
is an format(MM/DD/YYYY
) defined by dart.Here is an example
import 'package:intl/intl.dart';
main() {
DateTime dateTime = DateTime.now();
String timeFormat = DateFormat.yMd().format(dateTime);
print(timeFormat); //3/4/2022
}
Learned how to convert String date into DateTime in multiple ways.
🧮 Tags
Recent posts
Julia examples - Variable Type Nim example - Convert String to/from the Int How to get length of an array and sequence in Nim? Nim environment variables - read, set, delete, exists, and iterate examples? How to convert from single character to/from string in Nim?Related posts