我想知道是否可以从Moment对象中获取小时,分钟和秒吗?
例如:
import 'package:flutter/material.dart';
import 'dart:ui' as ui;
import 'package:flutter/services.dart' show rootBundle;
import 'dart:async';
import 'dart:typed_data';
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
ui.Image image;
bool isImageloaded = false;
GlobalKey _myCanvasKey = new GlobalKey();
void initState() {
super.initState();
init();
}
Future <Null> init() async {
final ByteData data = await rootBundle.load('img/guide1.png');
image = await loadImage( Uint8List.view(data.buffer));
}
Future<ui.Image> loadImage(List<int> img) async {
final Completer<ui.Image> completer = Completer();
ui.decodeImageFromList(img, (ui.Image img) {
setState(() {
isImageloaded = true;
});
return completer.complete(img);
});
return completer.future;
}
Widget _buildImage() {
ImageEditor editor= ImageEditor(image: image);
if (this.isImageloaded) {
return GestureDetector(
onPanDown: (detailData){
editor.update(detailData.localPosition);
_myCanvasKey.currentContext.findRenderObject().markNeedsPaint();
},
onPanUpdate: (detailData){
editor.update(detailData.localPosition);
_myCanvasKey.currentContext.findRenderObject().markNeedsPaint();
},
child: CustomPaint(
key: _myCanvasKey,
painter: editor,
),
);
} else {
return Center(child: Text('loading'));
}
}
@override
Widget build(BuildContext context) {
return _buildImage();
}
}
class ImageEditor extends CustomPainter {
ImageEditor({
this.image,
});
ui.Image image;
List<Offset> points=List();
final Paint painter = new Paint()
..color = Colors.blue[400]
..style = PaintingStyle.fill;
void update(Offset offset){
points.add(offset);
}
@override
void paint(Canvas canvas, Size size) {
canvas.drawImage(image, Offset(0.0, 0.0), Paint());
for(Offset offset in points){
canvas.drawCircle(offset, 10, painter);
}
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}
虽然我在文档中找不到类似的东西...
答案 0 :(得分:8)
请尝试以下操作:-
Moment('12:16','HH:mm').minutes();
更新:
此处的第二个选项-使用format('mm')
。它将以字符串格式返回'16'
。
Moment.js在link中记录了minutes()
函数。
P.S。未来的建议-在您的问题中更详细地说明。可能您的代码中有一些用例需要解决。如果人们对您的案件有描述,他们可能会以更优雅的方式帮助您解决案件。
祝您愉快善待并标记对您有用的答案是正确的答案!
答案 1 :(得分:2)
moment().minute(); // Number
moment().seconds(); // Number
文档:https://momentjs.com/docs/#/get-set/minute/
var now = moment().minutes();
alert(now);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>