我有时间选择器返回TimeOfDay对象但我已将该值保存在数据库中,作为从DateTime.millisecondsSinceEpoch
获得的毫秒整数答案 0 :(得分:7)
这是不可能的。
TimeOfDay
仅 小时和分钟。虽然DateTime
也有日/月/年
如果要转换它,则需要更多信息。比如当前的DateTime。然后将两者合并为一个最终日期时间。
TimeOfDay t;
final now = new DateTime.now();
return new DateTime(now.year, now.month, now.day, t.hour, t.minute);
答案 1 :(得分:4)
您可以使用DateTime扩展名
extension DateTimeExtension on DateTime {
DateTime applied(TimeOfDay time) {
return DateTime(year, month, day, time.hour, time.minute);
}
}
然后您可以像这样使用它:
final dateTime = yourDate.applied(yourTimeOfDayValue);
并将pubspec.yaml中的sdk版本更改为
environment:
sdk: ">=2.7.0 <3.0.0"
答案 2 :(得分:2)
您可以使用这种扩展名,也可以在单独的文件(如dateTime_extensions.dart)中添加更多扩展名方法,以使以后的项目工作变得轻松
文件: dateTime_extensions.dart;
extension DateTimeExtension on DateTime {
DateTime setTimeOfDay(TimeOfDay time) {
return DateTime(this.year, this.month, this.day, time.hour, time.minute);
}
DateTime setTime(
{int hours = 0,
int minutes = 0,
int seconds = 0,
int milliSeconds = 0,
int microSeconds = 0}) {
return DateTime(this.year, this.month, this.day, hours, minutes, seconds,
milliSeconds, microSeconds);
}
DateTime clearTime() {
return DateTime(this.year, this.month, this.day, 0, 0, 0, 0, 0);
}
///..... add more methods/properties for your convenience
}
像这样使用它
import 'package:your_app/dateTime_extensions.dart';
date.clearTime(); //to clear timeSpan
date.setTime(); //also be used to clear time if you don't provide any parameters
date.setTime(hours: 16,minutes: 23,seconds: 24); // will set time to existing date eg. existing_date 16:23:24
date.setTimeOfDay(TimeOfDay(hour: 16, minute: 59));
答案 3 :(得分:0)
这只是连接 var options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [100, 19, 3, 5, -10, 3],
borderColor: 'red',
backgroundColor: 'red'
},
{
label: '# of Votes2',
data: [20, 10, 4, 3, -30, 32],
borderColor: 'blue',
backgroundColor: 'blue'
}
]
},
options: {
plugins: {
backgrounds: {
hbars: [{
from: 0,
to: 'MIN',
color: "rgb(230, 195, 195)"
}]
}
}
},
plugins: [{
id: 'backgrounds',
beforeDraw: (chart, args, options) => {
const {
canvas,
ctx,
chartArea,
scales: {
y
}
} = chart;
let from = 0;
let to = 0;
options.hbars.forEach((hBar) => {
from = hBar.from;
to = hBar.to;
from = from === 'MIN' ? y.min : from;
from = from === 'MAX' ? y.max : from;
to = to === 'MIN' ? y.min : to;
to = to === 'MAX' ? y.max : to;
ctx.save(canvas.width, canvas.height);
ctx.fillStyle = hBar.color;
ctx.fillRect(chartArea.left, y.getPixelForValue(from), chartArea.right - chartArea.left, y.getPixelForValue(to) - y.getPixelForValue(hBar.from));
ctx.restore();
})
}
}]
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
和 <body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.4.0/chart.js"></script>
</body>
的一个小方法:
DateTime