Unix Time in Flutter
Tags: flutter, dart, codenewbie, 100daysofcode
Published: 2021-06-09 20:04:51

I had the privilege to work for an organization and develop a flutter application which communicates with an IoT Device. While developing the application, I was asked to use Unix Time, which otherwise called as Epoch Time.

My research taught me that 

##### "Unix time is a system for describing a point in time. It is the number of seconds that have elapsed since the Unix epoch, minus leap seconds; the Unix epoch is 00:00:00 UTC on 1 January 1970; leap seconds are ignored, with a leap second having the same Unix time as the second before it, and every day is treated as if it contains exactly 86400 seconds" - source Wikipedia.

So, how do we implement it in Flutter using Dart?

Well the answer is pretty straight forward, we have inbuilt functions in dart to do the required.

```
void main(){
DateTime now = DateTime.now();
var epochTime= now.millisecondsSinceEpoch;
print(epochTime);
}
```
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t4vvuoad8cvehzpdkq7x.png)
 
The output we get after the above would be in milliseconds i.e. there will be 13 digits and in decimal format.

If you need the output in seconds then , just divide the output by 1000, to fix it to 10 digits

```
DateTime now = DateTime.now();
var epochTime= now.millisecondsSinceEpoch/1000;
```

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2iwtdg9d65q47gubaaoh.png)
 
certainly can use **floor, ceil and round** methods if required to refractor the above values.

```
void main(){
DateTime now = DateTime.now();
var epochTime= (now.millisecondsSinceEpoch)/1000;
var testFloor= epochTime.floor();
var testCeil = epochTime.ceil();
var testRound = epochTime.round();
print(epochTime);
print("Testing Floor - " +  testFloor.toString());
print("Testing Ceil - " +  testCeil.toString());
print("Testing Round - " +  testRound.toString());
}
```

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vogyn9ilt500sezb0w5y.png)
 

In case you need the value in microseconds, use the **microsecondsSinceEpoch** function.

```
void main(){
DateTime now = DateTime.now();
var epochTime= now.microsecondsSinceEpoch;
print(epochTime);
}
```
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y0nc57m2vng3dgg535ip.png)
 
Now, what if you want the value in different formats like **HexCode or Binary or Decimal**

Well though it sounds little complicated its simple using the **toRadixString()** function.

- To convert the time value into **Binary** you need to pass the parameter **"2"** to the above function.
- To convert the time value into **Decimal** code you need to pass the parameter **"10"** to the above function.
- To convert the time value into **Hexadecimal** code you need to pass the parameter **"16"** to the above function.

```
void main(){

// assign the output in decimal format and in seconds
var epochTime= (DateTime.now().millisecondsSinceEpoch)/1000;
  
//Using Floor methid to get absolute value
var convertToFloor = epochTime.floor();


//convert the value to Binary
var convertToBinary= convertToFloor.toRadixString(2); 

//convert the value to Decimal
var convertToDecimal= convertToFloor.toRadixString(10);

//convert the value to Hex Code
var convertToHex= convertToFloor.toRadixString(16);

//print all
print("The Epoch Time in seconds is "+ epochTime.toString());
print("Using Floor to get absolute value " + convertToFloor.toString());
print("Converted to Binary - " + convertToBinary);
print("Converted to Decimal - " + convertToDecimal);
print("Converted to Hex - " + convertToHex);
}

```

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fxkdsgvrp5fmrlai5mx1.png)


That's it for this article, hope you found this article helpful.

Though its simple, I would like to know your thoughts in comments. 

Until we meet again with my next article, **Happy Coding** :smiley: