datetime

A Dart function to get the current date/time in a “seconds since the epoch” format

As a brief note, if you need a Dart function to get the current date/time in a “seconds since the epoch” format, I can confirm that this function works:

/// the current time, in “seconds since the epoch”
static int currentTimeInSeconds() {
    var ms = (new DateTime.now()).millisecondsSinceEpoch;
    return (ms / 1000).round();
}

A Flutter function to convert a TimeOfDay to a String (formatted)

If you ever need a Dart/Flutter method to format a TimeOfDay variable — i.e., convert a TimeOfDay to a String — I can confirm that this method works:

String formatTimeOfDay(TimeOfDay tod) {
    final now = new DateTime.now();
    final dt = DateTime(now.year, now.month, now.day, tod.hour, tod.minute);
    final format = DateFormat.jm();  //"6:00 AM"
    return format.format(dt);
}

Using a SQLite date/time field with Flutter and Dart

As a brief note, SQLite doesn’t have date/time (datetime) fields, so when you want to use a datetime field with Flutter (and Dart), you have to do something else.

My choice was to store my datetime data as a SQLite INTEGER field. I did this so I can query and sort that field easily myself. (The benefit of storing a datetime field as a TEXT field is that you can read it easier, but I think an INTEGER field is a little easier to work with, though YMMV.)

SQLite: Default a datetime field to the current time (now)

SQLite FAQ: How do I default a SQLite datetime field to the current date and time? (i.e., how do I default it to now?)

Just use the SQLite current_timestamp function, like this:

last_updated datetime default current_timestamp

In a more complete create table example I just used on an Android project, this looks like this:

Update MySQL timestamp on update

MySQL timestamp FAQ: How can I update a MySQL TIMESTAMP field when I issue an update for a MySQL database table?

MySQL has some crazy rules about how timestamp fields can be created, but one nice "timestamp update" syntax you can use looks like this, assuming that you're declaring a timestamp field named last_updated:

An apology, and a good laugh

I just wanted to offer a quick apology to everyone that has sent me emails lately. Yes, I have received your messages, and I will get to them as soon as possible. I'm just a bit swamped right now, so please bear with me, and I'll get to them all as soon as possible.

In the meantime here's a fun exchange from the MySQL support pages:

1) From Person A: It's a bug that I can't write sql/ddl like this:

start_date date not null default now(),

2) Reply from a support person: