0

I have this value (int):

230523110326

Being:

  • 23 - Day
  • 05 - Month
  • 23 - Year
  • 11 - Hour
  • 03 - Minutes
  • 26 - Seconds

I want to turn it now into a DateTime, since i'm using this method

DateTime.fromMillisecondsSiceEpoch(230523110326, isUtf: true)

but he is returning me this

DateTime (1977-04-22 02:11:50.366Z).

Would anyone have a solution?

I already tried multiplying the value by 1000, but it still doesn't work.

I. Biraj
  • 23
  • 3

3 Answers3

1

If your input is already an integer, you can do integer arithmetic to extract the parts:

void main() {
  var timestamp = 230523110326;

  var components = <int>[];
  for (var i = 0; i < 6; i += 1) {
    components.add(timestamp % 100);
    timestamp = timestamp ~/ 100;
  }

  var [seconds, minutes, hour, twoDigitYear, month, day] = components;
  var year = fromTwoDigitYear(twoDigitYear); // See below.

  var dateTime = DateTime(year, month, day, hour, minutes, seconds);
  print(dateTime); // Prints: 2023-05-23 11:03:26.000
}

See https://stackoverflow.com/a/76273568/ for how to convert a two-digit year to a four-digit yaer.

Finally, I'd like to point out that it's unclear whether your timestamp format is supposed to be in a ddMMyy or in a yyMMdd format because your particular example ambiguously uses 23 for both the day and two-digit year. The code above assumes ddMMyy to match the order you listed, but yyMMdd would be more sensible for an integer timestamp since it would allow comparing (most) timestamps with an integer comparison. (However, the use of two-digit years instead of four-digit years means that such comparisons wouldn't work properly for years before 2000.)

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
0

My friend, fromMillisecondsSiceEpoch is not like what you're thinking. Read more about Epoch:

Easy explain:

In programming and time systems, the "epoch" generally refers to a fixed point in time that serves as a reference for calculating other time values. The epoch is often a specific date, a starting point, or a significant marker in the time calculation process.

In the case of DateTime.fromMillisecondsSinceEpoch in Flutter, the epoch typically refers to the "Unix epoch" or "Unix timestamp." The Unix epoch is a widely used time system in programming. It is defined as 00:00:00 UTC on January 1, 1970. The DateTime.fromMillisecondsSinceEpoch method expects you to provide a time value in milliseconds since the Unix epoch. It then returns a corresponding DateTime object for that moment in time.

What should do in your case?

Do by hand, use method String. substring(first, last) to get one by one date, month, year, hour, minute, second. Example:

String date = "230523110326". substring(0,2); // then convert to int as you like 
Nguyen family
  • 749
  • 2
  • 12
0
void main(List<String> args) {
  var value = 230523110326;
  var svalue = value.toString();
  var list = <int>[];
  for (var i = 0; i < svalue.length/2; i++) {
    var ss = svalue.substring(2*i, 2*i+2);
    if (i==2) ss = '20$ss';
    list.add(int.parse(ss));
  }
  var dt = DateTime(list[2],list[1],list[0],list[3],list[4],list[5]);
  print(dt);
  print('\nJob done!');
}

Output:

2023-05-23 11:03:26.000

Job done!