Some Fun With Date and Get-Date For File Generation
Table of Contents
Overview
Just going through an overview of how posts are named on this site. Meaning the file name. This is mostly for my own benefit. But, if it helps others organize their own sites, cool beans.
The site names the file names for posts using the date and time in the
format "%Y%m%d%H%M%S"
. This is so I don’t have any duplicates. I
will admit, this is a random post. This will not provide any other
explaination to justify why other than I like it this way. It’s managable
for now.
Granted, I will go over how to obtain date and time through PowerShell and Bash with Hugo.
PowerShell - Date/Time
To obtain date/time using PowerShell on Windows I’ll use the Get-Date
cmdlet. This is easy to use and it’s built in. Don’t need to install any
modules for it.
Command can be found below. This will get the date/time and output it using Unix formatting.
Get-Date -UFormat "%Y%m%d%H%M%S"
PowerShell/Hugo - Date/Time While Creating A New Post
The command below will create a new hugo post in the format that I have designated in the Overview. This will do the trick in PowerShell on Windows.
Hugo generally uses Markdown so I added the little file extension to remove any confusion regarding file formatting.
You’ll need to be in your Hugo site if you want this command to work as expected.
hugo new .\content\posts\$(Get-Date -UFormat "%Y%m%d%H%M%S").md
Bash - Date/Time
For Bash on Linux, obtaining the date/time is slightly different in regards to how it’s formatted. But, the command does what it needs to for the scope of this article.
The command below will output the date and the time in the format specified in the Overview.
date +"%Y%m%d%H%M%S"
Bash/Hugo - Date/Time Wile Creating A New Post
You do the same in Bash using Hugo as you would in PowerShell. Provided there isn’t any weirdness. Follows the standardized format for posts on this site.
You’ll need to be in your Hugo site if you want this command to work as expected.
hugo new ./content/posts/$(date +"%Y%m%d%H%M%S").md
So What Does It Output To?
Well, of the general commands will output to the following.
20210726110410
The Hugo commands will tell you that it created a file in the path that you specified. I would recommend updating your path should you not create posts in the same way that I do.
Conclusion
There you have it. Some of the ways that I create content. This will be a good reference should I need to remember this in the futures. Sometimes I go without posting for months and have to remember the commands.
Hope everyone has a wonderful day. Cheers!