3

Is there a standard way to represent a date as a single integer? (I only need to store dates, not full timestamps.)

If it matters, the reason I want to do this is because I'm storing the dates in a SQLite database on Android and would like to store them as numbers so they can be compared/sorted efficiently to return results from queries.

5
  • # of days since (for example) Jan 1 1999?
    – Linuxios
    Jan 13, 2014 at 21:10
  • @Linuxios - Theoretically the dates should only be from the current date forward, but if there's some sort of semi-"universal" standard, I'd like to use that. Jan 13, 2014 at 21:17
  • 1
    If you only care about <, ==, and > comparisons, you might consider YYYYMMDD (for example, today is 20140113), so it's both easily comparable and human-readable. But bdf's answer is also a good one (I might pick noon UTC as the arbitrary time). Jan 13, 2014 at 21:21
  • @ElizabethC: See en.wikipedia.org/wiki/Unix_time. My mistake, Midnight Jan 1 1970.
    – Linuxios
    Jan 13, 2014 at 21:26
  • @Keith - I had thought about YYYYMMDD, and that's probably what I'll use unless some other standard for storing only dates exists (or someone has a strong reason not to use YYYYMMDD). Jan 13, 2014 at 21:31

3 Answers 3

4

One good option might be YYYYMMDD, for example encoding today (Jan 13, 2014) as the integer 20140113.

Advantages:

  • It works for comparisons, as long as you only care about <, ==, and >;
  • It's reasonably human-readable;
  • It's compatible with the ISO 8601 standard.

Disadvantages:

  • It's not as easy to compute differences between dates;
  • SQLite won't recognize it as a date.

On the last point: The SQLite3 documentation says that SQLite3 has no specific storage types for dates and/or times. Instead, it recommends using one of:

  • TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
  • REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
  • INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.

all of which apparently can be processed using SQLite's built-in date and time functions.

The latter argues in favor of the solution in bdf's answer. Picking an arbitrary time within the specified day is admittedly problematic, but I suggest picking noon UTC is unlikely to cause too many problems as long as you're careful to use it consistently. (Noon UTC can be on a different day if your time zone offset is 12 hours or more, but that's not an issue for most of the world.)

2
  • YYYYMMDD was what I'd been thinking of as well, with the obvious disadvantage that I'd have to write my own function to extract the parts of the date. Good point about computing differences between dates...at the moment I'm not expecting to need to do that, but it's possible the need could arise later, though I'm not sure if it would be done at the database level. I was hoping that there would be a standard numeric format to store just the date, but since it appears there isn't, I might try Unix time in seconds for now, especially since many conversion functions exist. Jan 15, 2014 at 1:25
  • I added an implementation sample for JavaScript of this simple yet great solution stackoverflow.com/a/58556353/140927
    – Holtwick
    Oct 25, 2019 at 10:21
2

just set the time for every day to an arbitrary time of your choosing, such as 2 am.

Storing them as timestamps anyway might still be a good idea, since you'd have a lot more date formatting options.

4
  • 1
    I might pick noon UTC as the arbitrary time. It's not affected by Daylight Saving Time, and it's very likely to give you the same date that a local timestamp would give you (unless your time zone happens to be ±12 hours or more from UTC). Jan 13, 2014 at 21:23
  • I would very much prefer not to store arbitrary timestamps, because adding meaningless information seems like it might be a good way to introduce bugs or odd corner cases at some point later (though I could be wrong). Jan 13, 2014 at 21:29
  • I get that - but personally, I think think that since you want to be tracking days, using a date-y data type would be the best choice, instead of rolling your own. Just take a look at docs.oracle.com/javase/1.5.0/docs/api/java/text/… to see all the options you'd have for formatting your timestamps. What if, down the road, you'd want to display day of the week (monday, tuesday, saturday)? It'd be really simple to implement that if you stored your data as a timestamp in the first place.
    – bdf
    Jan 13, 2014 at 21:34
  • I'm actually using joda-time, probably in this case the LocalDate class, for date manipulation and display. The issue here though is storage of dates in a database, not how to display them (though obviously there are advantages to storing dates in a format that's readily accepted by date-related classes). Jan 15, 2014 at 0:50
0

An important detail to keep in mind are time zones. If e.g. your time falls in the gap between your time zone offset and GMT you might get unexpected results. So at first I propose we discuss date as the one visible to the user which is usually the one of the local time zone.

So if we assume local time, and we want to make use of the Date objects, there are 2 possible solutions, which I will present as JavaScript unit test style. First one is the one presented by Keith Thompson previously:

let date = new Date('1987-12-31T01:02:03')

let simpleDateInteger = (
  date.getFullYear() * 10000 +
  (date.getMonth() + 1) * 100 +
  date.getDate()
)

expect(simpleDateInteger).toBe(19871231)

let fromSimpleDateInteger = new Date(
  simpleDateInteger / 10000, // year
  simpleDateInteger / 100 % 100 - 1, // month
  simpleDateInteger % 100 // day
)

expect(fromSimpleDateInteger.toDateString()).toEqual(date.toDateString())

If you need more compact integers and each integer +1 representing the next day, i.e. a continuous representation you can go with this one:

let date = new Date('1987-12-31T00:01:02')

const DAY_IN_MILLISECONDS = 86400 * 1000

let timeZoneInMilliSeconds = date.getTimezoneOffset() * 60 * 1000

let continuousDateInteger = Math.floor(
  (date.getTime() - timeZoneInMilliSeconds) / DAY_IN_MILLISECONDS
)

expect(continuousDateInteger).toBe(6573)

let fromContinuousDateInteger = new Date(
  continuousDateInteger * DAY_IN_MILLISECONDS + timeZoneInMilliSeconds
)

expect(fromContinuousDateInteger.toDateString()).toEqual(date.toDateString())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.