Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
using ISO8601 format instead of custom
azivnercba457bd611d
diff --git a/migrations/0050__string_dates.sql b/migrations/0050__string_dates.sqlindex 0e214b1aec..6750f68952 100644--- a/migrations/0050__string_dates.sql+++ b/migrations/0050__string_dates.sql@@ -9,7 +9,7 @@ CREATE TABLE `sync_mig` (`sync_date` TEXT NOT NULL);INSERT INTO sync_mig (id, entity_name, entity_id, source_id, sync_date)- SELECT id, entity_name, entity_id, source_id, datetime(sync_date, 'unixepoch') || '.000' FROM sync;+ SELECT id, entity_name, entity_id, source_id, strftime('%Y-%m-%dT%H:%M:%S.000Z', sync_date, 'unixepoch') FROM sync;DROP TABLE sync;ALTER TABLE sync_mig RENAME TO sync;@@ -25,8 +25,8 @@ CREATE INDEX `IDX_sync_sync_date` ON `sync` (-- Options-UPDATE options SET opt_value = datetime(opt_value, 'unixepoch') || '.000' WHERE opt_name IN ('last_backup_date');-UPDATE options SET date_modified = datetime(date_modified, 'unixepoch') || '.000';+UPDATE options SET opt_value = strftime('%Y-%m-%dT%H:%M:%S.000Z', opt_value, 'unixepoch') WHERE opt_name IN ('last_backup_date');+UPDATE options SET date_modified = strftime('%Y-%m-%dT%H:%M:%S.000Z', date_modified, 'unixepoch');-- Event log@@ -38,7 +38,7 @@ CREATE TABLE `event_log_mig` ();INSERT INTO event_log_mig (id, note_id, comment, date_added)- SELECT id, note_id, comment, datetime(date_added, 'unixepoch') || '.000' FROM event_log;+ SELECT id, note_id, comment, strftime('%Y-%m-%dT%H:%M:%S.000Z', date_added, 'unixepoch') FROM event_log;DROP TABLE event_log;ALTER TABLE event_log_mig RENAME TO event_log;@@ -62,8 +62,8 @@ CREATE TABLE IF NOT EXISTS "notes_mig" (INSERT INTO notes_mig (note_id, note_title, note_text, is_protected, is_deleted, date_created, date_modified)SELECT note_id, note_title, note_text, is_protected, is_deleted,- datetime(date_created, 'unixepoch') || '.000',- datetime(date_modified, 'unixepoch') || '.000'+ strftime('%Y-%m-%dT%H:%M:%S.000Z', date_created, 'unixepoch'),+ strftime('%Y-%m-%dT%H:%M:%S.000Z', date_modified, 'unixepoch')FROM notes;DROP TABLE notes;@@ -87,8 +87,8 @@ CREATE TABLE IF NOT EXISTS "notes_history_mig" (INSERT INTO notes_history_mig (note_history_id, note_id, note_title, note_text, is_protected, date_modified_from, date_modified_to)SELECT note_history_id, note_id, note_title, note_text, is_protected,- datetime(date_modified_from, 'unixepoch') || '.000',- datetime(date_modified_to, 'unixepoch') || '.000'+ strftime('%Y-%m-%dT%H:%M:%S.000Z', date_modified_from, 'unixepoch'),+ strftime('%Y-%m-%dT%H:%M:%S.000Z', date_modified_to, 'unixepoch')FROM notes_history;DROP TABLE notes_history;@@ -141,7 +141,7 @@ CREATE TABLE IF NOT EXISTS "notes_tree_mig" (INSERT INTO notes_tree_mig (note_tree_id, note_id, note_pid, note_pos, prefix, is_expanded, is_deleted, date_modified)SELECT note_tree_id, note_id, note_pid, note_pos, prefix, is_expanded, is_deleted,- datetime(date_modified, 'unixepoch') || '.000'+ strftime('%Y-%m-%dT%H:%M:%S.000Z', date_modified, 'unixepoch')FROM notes_tree;DROP TABLE notes_tree;diff --git a/public/javascripts/dialogs/event_log.js b/public/javascripts/dialogs/event_log.jsindex 428bf1a41e..f08f96d08d 100644--- a/public/javascripts/dialogs/event_log.js+++ b/public/javascripts/dialogs/event_log.js@@ -18,7 +18,7 @@ const eventLog = (function() {listEl.html('');for (const event of result) {- const dateTime = formatDateTime(getDateFromTS(event.date_added));+ const dateTime = formatDateTime(parseDate(event.date_added));if (event.note_id) {const noteLink = link.createNoteLink(event.note_id).prop('outerHTML');diff --git a/public/javascripts/dialogs/note_history.js b/public/javascripts/dialogs/note_history.jsindex 3bbbfd2c8e..d3a61551ec 100644--- a/public/javascripts/dialogs/note_history.js+++ b/public/javascripts/dialogs/note_history.js@@ -27,7 +27,7 @@ const noteHistory = (function() {historyItems = await server.get('notes-history/' + noteId);for (const item of historyItems) {- const dateModified = getDateFromTS(item.date_modified_from);+ const dateModified = parseDate(item.date_modified_from);$("#note-history-list").append($('<option>', {value: item.note_history_id,diff --git a/public/javascripts/dialogs/recent_changes.js b/public/javascripts/dialogs/recent_changes.jsindex 8c58afd7ab..9591167813 100644--- a/public/javascripts/dialogs/recent_changes.js+++ b/public/javascripts/dialogs/recent_changes.js@@ -24,7 +24,7 @@ const recentChanges = (function() {const dayEl = $('<div>').append($('<b>').html(formatDate(dateDay))).append(changesListEl);for (const change of dayChanges) {- const formattedTime = formatTime(getDateFromTS(change.date_modified_to));+ const formattedTime = formatTime(parseDate(change.date_modified_to));const revLink = $("<a>", {href: 'javascript:',@@ -57,7 +57,7 @@ const recentChanges = (function() {const dayCache = {};for (const row of result) {- let dateDay = getDateFromTS(row.date_modified_to);+ let dateDay = parseDate(row.date_modified_to);dateDay.setHours(0);dateDay.setMinutes(0);dateDay.setSeconds(0);diff --git a/public/javascripts/utils.js b/public/javascripts/utils.jsindex ab03172be0..000eae89ec 100644--- a/public/javascripts/utils.js+++ b/public/javascripts/utils.js@@ -36,10 +36,13 @@ function throwError(message) {throw new Error(message);}-function getDateFromTS(timestamp) {- // Date accepts number of milliseconds since epoch so UTC timestamp works without any extra handling- // see https://stackoverflow.com/questions/4631928/convert-utc-epoch-to-local-date-with-javascript- return new Date(timestamp * 1000);+function parseDate(str) {+ try {+ return new Date(Date.parse(str));+ }+ catch (e) {+ throw new Error("Can't parse date from " + str + ": " + e.stack);+ }}function formatTime(date) {diff --git a/services/utils.js b/services/utils.jsindex bb6bcd0dc5..4097aefe9a 100644--- a/services/utils.js+++ b/services/utils.js@@ -32,18 +32,16 @@ function nowDate() {}function dateStr(date) {- return date.toISOString().replace("T", " ").replace("Z", "");+ return date.toISOString();}/**- * @param str - needs to be in the "YYYY-MM-DD HH:MM:SS.sss" format as outputted by dateStr().- * also is assumed to be GMT time, *not* local time+ * @param str - needs to be in the ISO 8601 format "YYYY-MM-DDTHH:MM:SS.sssZ" format as outputted by dateStr().+ * also is assumed to be GMT time (as indicated by the "Z" at the end), *not* local time*/function parseDate(str) {try {- const isoDate = str.replace(" ", "T") + "Z";-- return new Date(Date.parse(isoDate));+ return new Date(Date.parse(str));}catch (e) {throw new Error("Can't parse date from " + str + ": " + e.stack);