Skip to content

Commit

Permalink
fix timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
Nriver committed Apr 15, 2024
1 parent ed5b4d6 commit 69cf5fe
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions countdown-days.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
const i18n = key => translations.trans[config.lang][key];

function parseDateInLocalTimezone(dateString) {
var parsedDate = new Date(dateString);
// Get the offset between local timezone and UTC (in milliseconds)
var offset = new Date().getTimezoneOffset() * 60 * 1000;
var targetTimestamp = parsedDate.getTime() + offset;
var targetDate = new Date(targetTimestamp);
return targetDate;
}

function calculateDays(dateString) {
// calculate day difference and show different message
var targetDate = new Date(dateString);
var targetDate = parseDateInLocalTimezone(dateString);
var today = new Date();
var timeDiff = targetDate.getTime() - today.getTime();
var dayDiff = Math.ceil(timeDiff / (1000 * 3600 * 24));
Expand All @@ -26,7 +35,7 @@ class countDownWidget extends api.NoteContextAwareWidget {
isEnabled() {
return super.isEnabled();
}

doRender() {
this.$widget = $(`
<style>
Expand All @@ -42,15 +51,15 @@ top: -10px;
async refreshWithNote(note) {
$(document).ready(function () {
// only enable in date note
if (!note.hasLabel("dateNote")){
if (!note.hasLabel("dateNote")) {
$('.countdown-div').remove();
return;
}

var noteDate = note.getLabelValue("dateNote");
var message = calculateDays(noteDate);
console.log(message);

// put the message below note title
var container = $("div.note-split:not(.hidden-ext) .note-title-widget");
if (!container.children().hasClass('countdown-div')) {
Expand All @@ -65,7 +74,7 @@ top: -10px;
}
});
}

}

module.exports = new countDownWidget();

0 comments on commit 69cf5fe

Please sign in to comment.