0

Is there a function or a way to convert "01 January 2020" , this form of date to milliseconds since epoch?

Currently I am using this approach :

Map months = {
  "Jan": 1,
  "Feb": 2,
  "Mar": 3,
  "May": 5,
  "Jun": 6,
  "Jul": 7,
  "Aug": 8,
  "Sep": 9,
  "Oct": 10,
  "Nov": 11,
  "Dec": 12,
};

date = "01 January 2020";
var millis = DateTime(int.parse(date!.substring(date.length - 4, date.length)), months[date.substring(3,6)] ,
int.parse(date.substring(0, 2));

I don't think it is the best approach to use .

  • want to convert the date into millisecond from the current date? – Nehil Koshiya Dec 23 '22 at 05:08
  • You need to create a date formatted using `intl` package. That format should match the date string format, and the you can get `DateTime` by using `customDateFormat.parse(date)` – ASAD HAMEED Dec 23 '22 at 05:11
  • See [How do I convert a date/time string to a DateTime object in Dart?](https://stackoverflow.com/q/49385303/) first. Once you have a `DateTime` object, you can simply use [`DateTime.millisecondsSinceEpoch`](https://api.dart.dev/stable/dart-core/DateTime/millisecondsSinceEpoch.html). – jamesdlin Dec 23 '22 at 05:28

3 Answers3

1

Did it using this approach :

var value1 = "01 January 2020";
var dateTimeFormat_v1 =DateFormat('dd MMMM yyyy', 'en_US') .parse(value1.toString());
print(dateTimeFormat_v1.millisecondsSinceEpoch);

0

In order to get millisecondsSinceEpoch from a formatted date string, you need to convert the formatted string to a DateTime object.

To achieve this, create a DateTime format that matches your date string, using the intl package. For your case, the formatter is;

DateFormat customDateFormat = DateFormat('dd MMMM yyyy');

Then parse the date string using customDateFormat, and then you'll get a DateTime object, which gives you millisecondsSinceEpoch

DateTime dateTime = customDateFormat.parse(dateString);
dateTime.millisecondsSinceEpoch
ASAD HAMEED
  • 2,296
  • 1
  • 20
  • 36
0

This was somewhat tricky but I managed to make it work, this is a pretty straightforward approach to get your answer:

void main() {
  String date = "1 January 2022";
  print(convert(date));
}

int convert(String date){
  List<String> a = [];
   a = date.split(" ");
  List month = ["January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December"];
  String converted = "${a[2]}-${(month.indexOf(a[1])+1).toString().length==1 ? "0${month.indexOf(a[1])+1}" : "${month.indexOf(a[1])+1}"}-${a[0].length==1?"0${a[0]}": "${a[0]}"}";
  DateTime a2 = DateTime.parse(converted);
  print(converted);
  return a2.microsecondsSinceEpoch;
}
Risheek Mittal
  • 1,077
  • 2
  • 18
  • check https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html - "It allows the user to choose from a set of standard date time formats as well as specify a customized pattern under certain locales" – pskink Dec 23 '22 at 05:38
  • I get it but if I can convert the date without using another dependency then I will and even if I use intl I'd still need to firstly convert **1 January 2022** to **2022-01-01** or something like that – Risheek Mittal Dec 23 '22 at 05:40
  • `intl` is a std flutter package for i18n and no, you don't have to convert it, just pick a correct format – pskink Dec 23 '22 at 05:42
  • Umm so why not just skip a dependency and use a 4 line function instead? – Risheek Mittal Dec 23 '22 at 05:44
  • 1
    this is a typical "reinventing the wheel" (even you said it is tricky), why to use 4 line workaround if you can do that in 1 line - see https://docs.flutter.dev/development/accessibility-and-localization/internationalization#using-the-dart-intl-tools – pskink Dec 23 '22 at 05:45
  • Sorry I still don't understand your point I'll be picking 4 lines of code over a whole package any day – Risheek Mittal Dec 23 '22 at 05:48
  • Well adding a package gets bundled in your app and cost you more than these **non-complex** lines of code lol. it was tricky cause i wanted a O(1) complexity which is even better than that 1 line of code believe it or not :p – Risheek Mittal Dec 23 '22 at 05:59
  • You can fact check everything yourself bruhh I'm not lying – Risheek Mittal Dec 23 '22 at 06:00
  • *Correctness* trumps efficiency. Can someone trust your implementation to be correct? Can someone trust it to be *robust*? This implementation will *silently* generate an incorrect `DateTime` if the input string doesn't exactly match those month names. – jamesdlin Dec 23 '22 at 07:20
  • So can intl correctly deduce if someone wrote `1 Janyart` instead of `1 January`? – Risheek Mittal Dec 23 '22 at 07:22
  • So can intl correctly deduce if someone wrote 1 Janyart instead of 1 January? Also nobody writes their dates it is always selected using a dropdown or a calendar date picker can you please calm down and understand that this is the better way of reducing complexities and size as well – Risheek Mittal Dec 23 '22 at 07:29
  • umm so you don't want the efficient method and just want your new method even though this one is superior and already available for you. Great. I have nothing more to discuss here then. This shows how closed you are to feedback or maybe you might think why should I listen to something from a person who is inferior to me in reputation – Risheek Mittal Dec 23 '22 at 09:22
  • "So can intl correctly deduce if someone wrote 1 Janyart instead of 1 January?" The point is that using `DateFormat` would fail in a detectable way rather than failing silently and generating an incorrect result. And if dates are *always* selected using a date picker, then there shouldn't be a need to parse such strings; just use `DateTime` objects directly (or serialize and deserialize ISO 8601 datetime strings). Additionally, if you're dealing with strings, consider what would happen if they're not in English. – jamesdlin Dec 23 '22 at 09:45
  • this is not "my new method" - [intl](https://pub.dev/packages/intl) package is written by "dart.dev" team - the guys who wrote the other core dart packages - do you think they do not know what they do? – pskink Dec 23 '22 at 09:52
  • @pskink writing a logic to a problem will always be a better solution than using a pre-written package it is that one fact about programming doesn't matter if dart or not – Risheek Mittal Dec 23 '22 at 10:29
  • @jamesdlin adding a custom dropdown is always an option to make it easier for the end user to understand. I can use custom strings for that dropdown and this is a really versatile answer that I've given over here. You can add anything you want for the month part **Jan**, **January**, **Janua** or even just a **J** all of those things can and will work for this. **I'm not saying that writing own code is better than using a package all the time.** Just for this scenario I'm implying that my solution is better than using a pre-defined library. That's all. – Risheek Mittal Dec 23 '22 at 10:36
  • Also I don't want to argue or anything please – Risheek Mittal Dec 23 '22 at 10:37