-
-
Notifications
You must be signed in to change notification settings - Fork 211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there a "better" way, using a user template, to get the last day of the month ? #93
Comments
Ok, well, after more research I've found another way to do this which is compatible with the plugins Calendar/Periodic notes 😊 . So instead of using today's date (at the moment of creation of the note) to get the last day of the month, I use the "date" from the title of note 😊 (Just in case, I would need to create a note in the past, or the future, through the Calendar). My user template is now looking like this :
... and I'm going to close this issue 😉 ! |
@I-Pch This is pretty cool & clever usage of You can do it in JS also, which runs faster. Paste this code into a template file and execute it with or without a date selected in the editor, it should work: <%*
function pad(n) {
return n.toString().padStart(2, "0");
}
function fmtDate(d) {
var yyyy = d.getFullYear();
var mm = pad(d.getMonth() + 1);
var dd = pad(d.getDate());
var dArr = [ yyyy, mm, dd ];
return(dArr.join('-'));
}
function lastDay(a) {
if (typeof(a) === 'undefined' || a === '') {
var d = new Date();
} else {
var d = new Date(a + ' 12:00:00');
if (d.toString() === 'Invalid Date') {
console.log('invalid date');
return
}
}
var lastDay = new Date(d.getFullYear(),d.getMonth()+1,0);
lastDate = fmtDate(lastDay)
lastDay = lastDay.getDate()
return lastDate // change to "lastDay" to output day only
} %><% lastDay(tp.file.selection())%> Last.Day.of.Month.mp4(the dimming parts of the video are me activating the template window) |
Hi @luckman212 😁 ! Thank you so very much for this very clean and clear example of using JS 👍 !!! This will definitively help me to expand my use of Templater 🥳 ! (I'm not very used to JS, but I definitively understand it better than the Terminal 😆 ) Since I've opened and closed this issue, someone on discord gave me another idea which uses directly Moment.js (and it's also definitively easier to understand for me 😊 ) So, to get the last day of the month I use this in the
But, I'm going to play around with your example and immerse myself in JS (now that I have a real nice working example 👍 ) because I'm pretty sure it will help me to correct things in my templates that were still bothering me 😊 ! |
Hi there 😊 !
So, first I really need to precise than I'm absolutely no dev' 😇 (please, bear with me 😇 )
Since I've discovered this magistral update on Templater (Thank you SilentVoid13 👍 ), I've spend a lot of time here trying to create a user template to automatically get me the
Last day of this month
😊 .It took me a while to discover I needed to use a
Terminal Command
(well, that's the only way I was able to make it work, after quite some research and vain trials looking in the direction of javascript 😇 )Right now my working template look like this :
It works and do what I need, but it seems a little slow (😇 ).
So, just in case I missed something (on how to write a user template), I just would like to know if there was a better way to get to the same result ? 😊
If anyone has any idea 💡 , I'm all ears 😊 !
Thank you very much in advance 👍 !!!
Edit: Changed the command, following the suggestion from another issue (#92 (comment))
The text was updated successfully, but these errors were encountered: