Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

I've been working on a SQLite backend for my Travel µBlog application when I came to the part that needed to deal with date/time formats. I was transitioning from a pure MySQL backend and learned that even though SQLite uses a very similar date/time format string, there are some subtle differences that can trip you up.
sqldev_ddl.png


The date/times that I was wanting to deal with were in the ISO 8601 format. Specifically, this is what a sample date/time string would look like:
 ISO 8601 Date
2017-05-14T18:16:01.001Z


In my case, as seen above, I was storing the date, time and fractional seconds. The time component was in the UTC time zone.

MySQL

To produce the above with MySql date and time functions the format string looked like this:
 MySQL ISO 8601 format string
%Y-%m-%dT%H:%i:%s.%fZ




SQLite

Moving to SQLite date functions, the format string became:
 MySQL ISO 8601 format string
%Y-%m-%dT%H:%M:%fZ


On the most part the format strings were similar. In fact the date component of the format string was identical. The time component was different however and this is where I tripped up initially. With SQLite, the 'fractional seconds' field was represented by a single substitution (%f) instead of two as used in MySQL (%s.%f).

So lesson learned! It's definitely worthwhile reading documentation before making any assumptions about date/time format strings, whether it be for MySQL, SQLite or any other database or programming language.

-i

A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. Use of any information from my blog posts should be at own risk and I do not hold any liability towards any information misuse or damages caused by following any of my posts.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
Hi! You can search my blog here ⤵
NOTE: (2022) This Blog is no longer maintained and I will not be answering any emails or comments.

I am now focusing on Atari Gamer.