mirror of
https://github.com/zadam/trilium.git
synced 2025-03-05 03:25:59 +08:00
use local dates to create day notes, closes #337
This commit is contained in:
parent
cce8c1b674
commit
bc4cec69a5
3 changed files with 31 additions and 13 deletions
|
@ -81,7 +81,7 @@ app.on('ready', async () => {
|
|||
const dateNoteService = require('./src/services/date_notes');
|
||||
const dateUtils = require('./src/services/date_utils');
|
||||
|
||||
const parentNote = await dateNoteService.getDateNote(dateUtils.nowDate());
|
||||
const parentNote = await dateNoteService.getDateNote(dateUtils.nowLocalDate());
|
||||
|
||||
// window may be hidden / not in focus
|
||||
mainWindow.focus();
|
||||
|
|
|
@ -47,8 +47,8 @@ async function getRootCalendarNote() {
|
|||
return rootNote;
|
||||
}
|
||||
|
||||
async function getYearNote(dateTimeStr, rootNote) {
|
||||
const yearStr = dateTimeStr.substr(0, 4);
|
||||
async function getYearNote(dateStr, rootNote) {
|
||||
const yearStr = dateStr.substr(0, 4);
|
||||
|
||||
let yearNote = await attributeService.getNoteWithLabel(YEAR_LABEL, yearStr);
|
||||
|
||||
|
@ -66,19 +66,19 @@ async function getYearNote(dateTimeStr, rootNote) {
|
|||
return yearNote;
|
||||
}
|
||||
|
||||
async function getMonthNote(dateTimeStr, rootNote) {
|
||||
const monthStr = dateTimeStr.substr(0, 7);
|
||||
const monthNumber = dateTimeStr.substr(5, 2);
|
||||
async function getMonthNote(dateStr, rootNote) {
|
||||
const monthStr = dateStr.substr(0, 7);
|
||||
const monthNumber = dateStr.substr(5, 2);
|
||||
|
||||
let monthNote = await attributeService.getNoteWithLabel(MONTH_LABEL, monthStr);
|
||||
|
||||
if (!monthNote) {
|
||||
const yearNote = await getYearNote(dateTimeStr, rootNote);
|
||||
const yearNote = await getYearNote(dateStr, rootNote);
|
||||
|
||||
monthNote = await getNoteStartingWith(yearNote.noteId, monthNumber);
|
||||
|
||||
if (!monthNote) {
|
||||
const dateObj = dateUtils.parseDate(dateTimeStr);
|
||||
const dateObj = dateUtils.parseLocalDate(dateStr);
|
||||
|
||||
const noteTitle = monthNumber + " - " + MONTHS[dateObj.getMonth()];
|
||||
|
||||
|
@ -92,21 +92,20 @@ async function getMonthNote(dateTimeStr, rootNote) {
|
|||
return monthNote;
|
||||
}
|
||||
|
||||
async function getDateNote(dateTimeStr) {
|
||||
async function getDateNote(dateStr) {
|
||||
const rootNote = await getRootCalendarNote();
|
||||
|
||||
const dateStr = dateTimeStr.substr(0, 10);
|
||||
const dayNumber = dateTimeStr.substr(8, 2);
|
||||
const dayNumber = dateStr.substr(8, 2);
|
||||
|
||||
let dateNote = await attributeService.getNoteWithLabel(DATE_LABEL, dateStr);
|
||||
|
||||
if (!dateNote) {
|
||||
const monthNote = await getMonthNote(dateTimeStr, rootNote);
|
||||
const monthNote = await getMonthNote(dateStr, rootNote);
|
||||
|
||||
dateNote = await getNoteStartingWith(monthNote.noteId, dayNumber);
|
||||
|
||||
if (!dateNote) {
|
||||
const dateObj = dateUtils.parseDate(dateTimeStr);
|
||||
const dateObj = dateUtils.parseLocalDate(dateStr);
|
||||
|
||||
const noteTitle = dayNumber + " - " + DAYS[dateObj.getDay()];
|
||||
|
||||
|
|
|
@ -2,6 +2,16 @@ function nowDate() {
|
|||
return dateStr(new Date());
|
||||
}
|
||||
|
||||
function nowLocalDate() {
|
||||
const date = new Date();
|
||||
|
||||
return date.getFullYear() + "-" + pad(date.getMonth() + 1) + "-" + pad(date.getDate());
|
||||
}
|
||||
|
||||
function pad(num) {
|
||||
return num <= 9 ? `0${num}` : `${num}`;
|
||||
}
|
||||
|
||||
function dateStr(date) {
|
||||
return date.toISOString();
|
||||
}
|
||||
|
@ -25,14 +35,23 @@ function parseDate(str) {
|
|||
return parseDateTime(datePart + "T12:00:00.000Z");
|
||||
}
|
||||
|
||||
function parseLocalDate(str) {
|
||||
const datePart = str.substr(0, 10);
|
||||
|
||||
// not specifying the timezone and specifying the time means Date.parse() will use the local timezone
|
||||
return parseDateTime(datePart + " 12:00:00.000");
|
||||
}
|
||||
|
||||
function getDateTimeForFile() {
|
||||
return new Date().toISOString().substr(0, 19).replace(/:/g, '');
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
nowDate,
|
||||
nowLocalDate,
|
||||
dateStr,
|
||||
parseDate,
|
||||
parseDateTime,
|
||||
parseLocalDate,
|
||||
getDateTimeForFile
|
||||
};
|
Loading…
Reference in a new issue