0

I have an app and in that app I have to show chart but when I get chart data from backhand it form in UTC but my client is from San Francisco so he receive has invalid data because of different timezone

So I want to create a function that can change time according to user local time zone

How can I create that?

Jahidul Islam
  • 11,435
  • 3
  • 17
  • 38
Dipak Ramoliya
  • 547
  • 11
  • 32
  • Does this answer your question? [DateTime in UTC not converting to Local](https://stackoverflow.com/questions/58322185/datetime-in-utc-not-converting-to-local) – Jahidul Islam Jan 19 '22 at 06:02
  • no because this question answers don't work for me – Dipak Ramoliya Jan 19 '22 at 06:18
  • @Dipak If the `DateTime` object was constructed as UTC, then `.toLocal()` will convert it to the system's local time. If *you* aren't using PST, then you will not see a PST time. If it's still not working, you're probably not constructing the `DateTime` object properly and should show your code. – jamesdlin Jan 19 '22 at 07:59

2 Answers2

0

you can use toLocal method. try below code

    var date = DateFormat("yyyy-MM-dd HH:mm:ss").parse(dateUtc, true);
    var local = date.toLocal().toString();
    print(local);
Shailendra Rajput
  • 2,131
  • 17
  • 26
0

This is the function that can change time according to user local time zone

void main() {
  var x = DateTime.now();
  print(x.year.toString() + "-" + padStart(x.month.toString()) + "-" + padStart(x.day.toString()));
}

String padStart(String str)
{
  return (str.length < 2) ? "0" + str: str;
}

Expected output

Abdullah Ansari
  • 618
  • 1
  • 4
  • 10
  • 1
    There's no need to write your own `pad` function. It's already included in `String`. See: https://api.dart.dev/stable/2.15.1/dart-core/String/padLeft.html – Richard Heap Jan 19 '22 at 11:12