[英]How to get date given only the day of year number in flutter
I am fairly new to flutter.我对颤振相当陌生。 In android, I could easily use the
Calendar
class set using cal.set(Calendar.DAY_OF_YEAR, number)
and it will return the date
for that particular day_of_year
number.在 android 中,我可以轻松地使用
cal.set(Calendar.DAY_OF_YEAR, number)
设置的Calendar
类cal.set(Calendar.DAY_OF_YEAR, number)
它将返回该特定day_of_year
数字的date
。 How can I do the same in Flutter.我如何在 Flutter 中做同样的事情。
In Android, this is what I would have done在 Android 中,这就是我会做的
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_YEAR,1); //set date using day of year
return cal.getTime(); //2020-01-01
How do I implement this in Flutter.我如何在 Flutter 中实现这一点。
I gave this a go for fun, I don't normally do Dart, so apologies if its wrong!我只是为了好玩,我通常不做 Dart,所以如果它错了,请道歉!
var dayOfYear = 1;
var millisInADay = Duration(days: 1).inMilliseconds; // 86400000
var millisDayOfYear = dayOfYear * millisInADay;
var millisecondsSinceEpoch = DateTime(DateTime.now().year).millisecondsSinceEpoch;
var dayOfYearDate = DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch + millisDayOfYear);
Create a new date for the first day of the year.为一年的第一天创建一个新日期。
Add the number of millis in the year, to the number of millis to the start of the year since epoch.将年份中的毫秒数与自纪元以来年份开始的毫秒数相加。 Create a new date using this milliseconds since epoch.
使用自纪元以来的毫秒数创建一个新日期。
Reference:参考:
https://api.dart.dev/stable/2.7.1/dart-core/DateTime-class.html https://api.dart.dev/stable/2.7.1/dart-core/DateTime-class.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.