flutter convert milliseconds to date

For more articles, please see flutter from entry to mastery

The common_utils tool class has encapsulated the DateUtil tool class to handle common date and time data formatting

common_utils: ^1.1.1
  • 1

1 Operations on time data

    //Get the milliseconds of the current time 
    int nowDateMilliseconds = DateUtil . getNowDateMs ( ) ; 
    print ( "nowDateMilliseconds: " + nowDateMilliseconds . toString ( ) ) ; 
    //I/flutter ( 7076): nowDateMilliseconds: 1562485927175
    
    //Get the current time 
    String nowDateStr = DateUtil . getNowDateStr ( ) ; 
    print ( "nowDateStr: " + nowDateStr . toString ( ) ) ; 
    //I/flutter ( 7076): nowDateStr: 2019-07-07 15:52:07
    
    //Get the week corresponding to the specified time 
    String zhWeekDayByMs = DateUtil . getZHWeekDayByMs ( 1562484092000 ) ; 
    print ( "zhWeekDayByMs: " + zhWeekDayByMs . toString ( ) ) ; 
    //I/flutter ( 7076): zhWeekDayByMs: Sunday
    
    //Convert date to milliseconds 
    int dateMsByTimeStr = DateUtil . getDateMsByTimeStr ( "2019-07-07 15:21:32" ) ; 
    print ( "dateMsByTimeStr: " + dateMsByTimeStr . toString ( ) ) ; 
    //I/flutter ( 7076) : dateMsByTimeStr: 1562484092000
    
    //Convert milliseconds to date format 
    String dateStrByMs = DateUtil . getDateStrByMs ( 1562484092000 ) ; 
    print ( "dateStrByMs: " + dateStrByMs . toString ( ) ) ; 
    //I/flutter ( 7076): dateStrByMs: 2019-07-07 15: 21:32
    
    // Convert milliseconds to date format Specify date format 
    var dateStrByMs2 = DateUtil . getDateStrByMs ( 1562484092000 , format : DateFormat . HOUR_MINUTE ) ; 
    print ( "dateStrByMs2: " + dateStrByMs2 . toString ( ) ) ; 
    //I/flutter ( 7076): dateStrByMs2: 15:21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

2 Calculate the milliseconds of a specified date as just, xx minutes ago, xx hours ago...

    //Specify the date and time 
    var format5 = TimelineUtil . format ( 1562485553000 ) ; 
    var format2 = TimelineUtil . format ( 1562484092000 ) ; 
    var format4 = TimelineUtil . format ( 1562466092000 ) ; 
    var format3 = TimelineUtil . format ( 1562311292000 )

    print("format "+format5);
    print("format "+format2);
    print("format "+format4);
    print("format "+format3);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

log output

I / flutter (  7076 ) : format 1 minute ago
I / flutter (  7076 ) : format 26 minutes ago
I / flutter (  7076 ) : format 5 hours ago
I / flutter (  7076 ) : format 2 days ago
  • 1
  • 2
  • 3
  • 4

Tags: flutter convert milliseconds to date

A full set of tutorials for Flutter project development

Related: flutter convert milliseconds to date