0

I'm building a weather app and I'm fetching weather and other information from openweathermap.org. I would like to get sunrise and sunset information for each of the searched location but I'm not sure how to change the format from seconds into hh:mm

My code is:

document.querySelector("#sunrise").innerHTML = ${ DateTime.fromMillisecondsSinceEpoch(response.data.sys.sunrise) * 1000 };

I tried searching online and came with the above but it doesn't work.

1 Answers1

0

After you get your DateTime from fromMillisecondsSinceEpoch function, you can print the format in hh:mm as:

DateTime sunrise = DateTime.fromMillisecondsSinceEpoch(response.data.sys.sunrise) * 1000 
DateFormat formatter = DateFormat('hh:mm a');
String formatted = formatter.format(sunrise);

This code will produce an output like this:

10:05 PM

Or it you would want to use 24 our format use HH:mm instead of hh:mm a

ashish.g
  • 525
  • 11
  • 25