flutter时间日期格式及日期选择组件

一.flutter中的日期转换工具类

class TimeUtils {

  //获取现在的时间戳
  static int getDayNow(){
    var nowTime = DateTime.now();//初始化当前时间日期
    return nowTime.millisecondsSinceEpoch;//返回时间戳
  }

  //将时间戳转换为时间日期
  static String getTimeEpoch(int millisecondsSinceEpoch){
    return DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch).toString();//返回时间戳
  }

  //获取当前时间日期显示
  static String getCurrentTime(){
    var nowTime = DateTime.now();
    String currentTime=formatDate(nowTime, [yyyy,'年',mm,'月',dd,' ',HH,':',nn]);
    return currentTime;
  }

}

二.使用第三方插件进行日期转换

1.pubspec.yaml中添加依赖,并在android studio中点击pub get更新插件

dependencies:
  flutter:
    sdk: flutter
  date_format: ^2.0.5

2.代码中引用包

import 'package:date_format/date_format.dart';

3.插件使用

1).中文日期格式,eg:

static String getCurrentTime(){
    var nowTime = DateTime.now();
    String currentTime=formatDate(nowTime, [yyyy,'年',mm,'月',dd,' ',HH,':',nn]);
    return currentTime;
  }

2).其他日期格式

formatDate(DateTime(2022, 01, 14), [yyyy, '-', mm, '-', dd]) //2022-01-14
formatDate(DateTime(2022, 1, 14), [yy, '-', M, '-', d]) //22-jan-14
formatDate(DateTime(2022, 01, 14, 15, 40, 10), [HH, ':', nn, ':', ss]) //15:40:10
formatDate(DateTime(2022, 02, 4, 15, 40, 10), [HH, ':', nn, ':', ss, z]) //15:40:10+0100

三.参考链接

1.日期格式化插件:日期格式化插件date_format

2.日期选择组件,可参考:flutter_cupertino_date_picker

3.插件及插件版本搜索:插件版本查询

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值