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

Skip down to comments...
Hope you found this post useful...

...so please read on! I love writing articles that provide beneficial information, tips and examples to my readers. All information on my blog is provided free of charge and I encourage you to share it as you wish. There is a small favour I ask in return however - engage in comments below, provide feedback, and if you see mistakes let me know.

If you want to show additional support and help me pay for web hosting and domain name registration, donations, no matter how small, are always welcome!

Use of any information contained in this blog post/article is subject to this disclaimer.
 
comments powered by Disqus
Other posts you may like...