1

Say the EPOCH timestamp I received from an API is 1595216214. It is equivalent to Monday, July 20, 2020 3:36:54 AM (GMT).

My interest is time value only (Ignoring the date/day value)? How can I code in Dart? Also, how can I convert it into my time zone (E.g.: GMT+8)

iqfareez
  • 555
  • 7
  • 21
  • 3
    Does this answer your question? [Dart - Converting Milliseconds Since Epoch (UNIX timestamp) into human readable time](https://stackoverflow.com/questions/45357520/dart-converting-milliseconds-since-epoch-unix-timestamp-into-human-readable) – Yadu Jul 20 '20 at 04:23
  • basically a single search gives you this – Yadu Jul 20 '20 at 04:23

1 Answers1

-1

you can use DateTime class to do that. Like this:

var dateUtc = DateTime.fromMillisecondsSinceEpoch(myAPIEpochTimeInMilliseconds, isUtc: true);
var dateInMyTimezone = dateUtc.add(Duration(hours: 8));
var secondsOfDay = dateInMyTimezone.hour * 3600 + dateInMyTimezone.minute * 60 + dateInMyTimezone.second;

NOTE:

  1. If you are doing this for the web, although Dart does support 64+ bit numbers, javascript only takes 32-bit integers. So, use the BigInt class for big numbers tha exceeds 32-bit representation.

  2. DateTime doesn't have an inherent timezone to be defined on the class. Is either the local (machine) time or utc Time. So, it is recomended to always use utc and add timezone offset when needed. Or just create a wrapper.

rdnobrega
  • 721
  • 6
  • 16
  • It does not. If you have actually read, it does not answer the question. Plus, I explicitly added new information and complemented to a more complete response. Flagged. – rdnobrega Jul 20 '20 at 17:39