先前设置的DateTime对象在使用toUtc()时不获得值更新



我有一个方法叫做"showLotteryInfo"它使用一些datetime对象。在使用一些逻辑设置值之后,当我对它们执行. toutc调用时,这些值没有得到更新。谁能告诉我为什么?

void showLotteryInfo() {
//DateTime now = new DateTime.now();
DateTime nextLotteryDate = new DateTime.now();
DateTime startingLotteryDate = new DateTime.now();
if (DateTime.now().day < 15){
nextLotteryDate = DateTime(DateTime.now().year, DateTime.now().month, 15);
startingLotteryDate =  DateTime(DateTime.now().year, DateTime.now().month, 1); 
}else{
nextLotteryDate =  DateTime(DateTime.now().year, DateTime.now().month+1, 1);
startingLotteryDate =  DateTime(DateTime.now().year, DateTime.now().month, 15); 
}

DateTime nextLotteryDateUTC = nextLotteryDate.add(Duration(days: 50)); //this updates the datetime value
DateTime startingLotteryDateUTC = startingLotteryDate.toUtc(); //this DOES not update the datetime value
String endingFormattedDate = DateFormat('yyyy-MM-dd').format(nextLotteryDateUTC);
String startingFormattedDate = DateFormat('yyyy-MM-dd').format(startingLotteryDateUTC);

showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text("Lottery Info"),
//content: Text("Next Drawing Date: Lottery Info"),
content: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
//position
mainAxisSize: MainAxisSize.min,
// wrap content in flutter
children: <Widget>[
Text(
"Starting Date Range:",
style: TextStyle(fontWeight: FontWeight.bold),
),
Text(startingFormattedDate),
Text(
"Next Drawing Date:",
style: TextStyle(fontWeight: FontWeight.bold),
),
Text(endingFormattedDate)
],
),
actions: <Widget>[
new TextButton(
child: const Text("OK"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);

}

我正在使用Android Studio与Pixel 5 API 31模拟器

您可以使用:

DateTime startingLotteryDateUTC = 
TimeZoneInfo.ConvertTimeToUtc(startingLotteryDate, TimeZoneInfo.Local);

或:

DateTime startingLotteryDateUTC = DateTime.UtcNow

最新更新