Flutter / Dart Date and Time Formatter Functions

Job ID: 31741997

Budget: $10 – $30 USD

This project must use the DateTime class (https://api.flutter.dev/flutter/dart-core/DateTime-class.html). I'm looking to format the date and/or time based on the passed in value to the function. I need two different functions I outlined below. The formatting is similar to SMS client apps on your Smartphone. The first function is based on the Conversation view of all the chat in your SMS Inbox. The second function is based of a specific chat message with a contact.

I need a function to format the date/time based off milliseconds from epoch. The returned date or time should follow these rules.

- if the timestamp is today then it should return the "time" (e.g. 11:08 AM)
- if the timestamp is within one week of the given timestamp, excluding today, then it should return the "day of week" (e.g. Mon)
- if the timestamp is within one year of the given timestamp it should return the "month" and "day" (e.g. Sep 16)
- if the timestamp is older than one year of the given timestamp it should return "month", "day" and "year" (e.g. 9/8/20)

String formatConversationDate(int milliseconds) {
final DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(milliseconds);

// Example return
return "${dateTime.month}-${dateTime.day}";
}


I need a second function to format the date/time based off milliseconds from epoch. The returned date or time should follow these rules.

- if the timestamp is today then it should return the "time" (e.g. 11:08 AM)
- if the timestamp is within one week of the given timestamp, excluding today, then it should return the "day of week" and "time" (e.g. Mon 7:21 PM)
- if the timestamp is within one year of the given timestamp it should return the "month", "day" and "time" (e.g. Sep 28 12:10 PM)
- if the timestamp is older than one year of the given timestamp it should return "month", "day", "year" and "time" (e.g. 9/8/20 9:00 AM)

String formatChatMessageDate(int milliseconds) {
final DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(milliseconds);

// Example return
return "${dateTime.month}-${dateTime.day}";
}

You can use the link below to generate the needed epoch timestamps.

https://www.epochconverter.com/
Related categories: Dart Flutter