Skip to content

Instantly share code, notes, and snippets.

@Ciantic
Created April 24, 2019 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ciantic/f11395395c748ce2a7a10e1147a02aac to your computer and use it in GitHub Desktop.
Save Ciantic/f11395395c748ce2a7a10e1147a02aac to your computer and use it in GitHub Desktop.
node sqlite3 uses getTime (unixepoch as milliseconds) for default date to integer conversion
import sqlite3 from "sqlite3";
let db: any;
beforeEach(done => {
db = new sqlite3.Database(":memory:", () => {
db.run("CREATE TABLE foo (num INT)", done);
});
});
test("Should insert datetime as dateObject.getTime() and retrieve it as so", done => {
db.run(
"INSERT INTO foo VALUES($date)",
{
$date: new Date("2000-01-01T02:00Z")
},
(err: any) => {
db.all("SELECT * from foo", (err: any, rows: any) => {
expect(new Date("2000-01-01T02:00Z").getTime()).toEqual(946692000000);
expect(rows[0].num).toEqual(946692000000);
expect(new Date(rows[0].num)).toEqual(new Date("2000-01-01T02:00Z"));
done();
});
}
);
});
@Ciantic
Copy link
Author

Ciantic commented Apr 25, 2019

Note to myself, this is probably a side effect from Date to integer conversion in JavaScript engines, especially Chrome's. Putting this to console:

+(new Date("2000-01-01T02:00Z"))

Results in an integer 946692000000.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment