How to render correct datetime in hugo post? - blogs

I created a post using hugo new posts/mypost.md and it created mypost.md for me with header toml configuration like that
But, when i run on server (local), the datetime rendered wrong like:
How can I fix that?
Thanks in advance!

This is how I got it to work:
Add your date format in config.toml
[params]
dateFormat = "02 Jan 2006"
Your post should contain the date in its front-matter:
---
date: "2020-12-23T15:21:54+05:30"
...
---
Use the format in your layout:
<div>{{ .Params.date.Format .Site.Params.dateFormat }}</div>
Note: Please DO NOT change the numbers in the date format. It must be 02 for day, Jan for month, 2006 for year, etc. check this for more details.

Check also Hugo 0.87 (Aug. 2021, two years later), which comes with:
a timezone
a timedate format
{{ time.Format "Monday, Jan 2, 2006" "2015-01-21" }}
→
"Wednesday, Jan 21, 2015"
Note that since Hugo 0.87.0, time.Format will return a localized string for the current language.
Date/time formatting layouts
{{ .Date | time.Format ":date_long" }}
The full list of custom layouts with examples for English:
:date_full => Wednesday, June 6, 2018
:date_long => June 6, 2018
:date_medium => Jun 6, 2018
:date_short => 6/6/18
:time_full => 2:09:37 am UTC
:time_long => 2:09:37 am UTC
:time_medium => 2:09:37 am
:time_short => 2:09 am

In Hugo v0.95.0 (released 16 Mar 2022), it should be
[params]
date_format = "02 Jan 2006"
with
---
date: 2022-03-18T21:10:00+07:00
---
in the post front matter, where double quote marks are not necessary. I used Ananke theme.

Setting the date_format in the config file seems weird to me. I would set the following in the config file:
defaultContentLanguage: nl
languageCode: nl_NL
These are variables that do NOT go into params, but on the root level of the config file. When you have these you can simply call:
{{ .Date | time.Format ":date_long" }}
or... if you need another format:
{{ .Date | time.Format ":date_medium" }}
The date will be formatted in your language.

You need to set dateformat in file config.toml at 2.1.2006 (any proper format, ensure 2, Jan, 2006).
This link save my day

Related

Unknow HTML date format "1615856400000" = "Thuesday, Mar 16, 2021", "1615770000" = "Thuesday, Mar 15, 2021" etc

I'm web scraping a HTML page which shows a time series generated by a function inside a <script> tag. On the webbpage I can see different dates for the time series, but when I "view page source" these dates seem to be converted into numbers.
For example "1615856400000" = "Thuesday, Mar 16, 2021", "1615770000" = "Monday, Mar 15, 2021" and "1612227600000" = "Thuesday, Feb 2, 2021".
Does anyone know what the logic is for these conversions? Ideally I would like a list which shows the mappings per day, starting at some arbitrary date e.g. "Wednesday, Feb 26, 2014".

JSON string to date with Javascript in Google Apps Script editor

I am working through w3schools, particularly https://www.w3schools.com/js/js_json_parse.asp
I ran this example and got an unexpected result
let dashText = '{ "name":"John", "birth":"1986-12-14", "city":"New York"}';
let objD = JSON.parse(dashText);
console.log("objD: ", objD);
objD.birth = new Date(objD.birth);
console.log("objD.birth: ", objD.birth);
3:09:04 PM Info objD: { name: 'John', birth: '1986-12-14', city: 'New York' }
3:09:04 PM Info objD.birth: Sat Dec 13 1986 18:00:00 GMT-0600 (Central Standard Time)
Note the difference in the dates. I then changed the dashes to slashes out of curiosity and the date was correctly determined from the string.
let slashText = '{ "name":"John", "birth":"1986/12/14", "city":"New York"}';
let objS = JSON.parse(slashText);
console.log("objS: ", objS);
objS.birth = new Date(objS.birth);
console.log("objS.birth: ", objS.birth);
3:09:04 PM Info objS: { name: 'John', birth: '1986/12/14', city: 'New York' }
3:09:04 PM Info objS.birth: Sun Dec 14 1986 00:00:00 GMT-0600 (Central Standard Time)
Can anyone explain the results?
Javascript parses DateTime strings differently based on how the string is formatted. The dashes are parsed to ISO date, i.e. international time. You can see this when it tries to handle the timezone conversion, where it sets the time to 18:00:00 to account for the 6 hour shift from Universal Time. Slashes are parsed as just the date, and doesn't try to adjust the time based on timezones.
Here's a w3schools link that goes over this in more detail.

Change date format in my HTML / CSS output

Have a dates saved in my sqlite database in this format 2019-01-24 13:41:40.515955 and when I output to my web page its displayed as 2019-01-24 13:41:40 UTC. Please can I get guidance on displaying something like Wed 24 January 2019 ? No sure how to approach it
Use Javascript, would be a simple approach to parse and format dates with desired output.
var date = new Date('6/29/2011 4:52:48 PM UTC');
date.toString() // "Wed Jun 29 2011 09:52:48 GMT-0700 (PDT)"
var date = new Date('6/29/2011 4:52:48 PM UTC');
date.toString() // "Wed Jun 29 2011 09:52:48 GMT-0700 (PDT)"
console.log(`Year: ${date.getFullYear()}, Month: ${date.getMonth()}, Day, ${date.getDay()}`)
You can also parse dates using Date.parse(``);

Yii2 formatter language

This is my configurations:
'formatter' => [
'class' => 'yii\i18n\Formatter',
'dateFormat' => 'd MMMM Y',
'locale' => 'ru-RU'
],
When I am trying this:
echo Yii::$app->formatter->asDate('2014-01-01');
I get:
01 Jan 2014
But I want the same in Russian.
Solved:
Open the file php.ini via OpenServer and uncomment ;extension=php_intl.dll.
the config you specified should output 01 янв. 2014
you are most likely missing php-intl extension
With formatter set to 'dateFormat' => 'php:d M Y' you can only get 01 Jan 2014 because PHP's date M stands for
A short textual representation of a month, three letters: Jan through Dec
If you want date to be formatted by intl for your language you need to set formatter with ICU:
'dateFormat' => 'd MMM Y'
like described at ICU User Guide.

Jekyll: can't sort collection by date

This is making me crazy.
I have this collection resources:
# _config.yml
collections:
resources:
output: true
permalink: /resources/:name/
They all have dates:
# /_resources/example.md
---
title: Learn the Web
date: 09-04-2013
---
The pages get generated, and if I try to display it's date, it is displayed correctly, but I also want to sort those by date, and it just doesn't work. What am I doing wrong?
{% assign sortedResources = site.resources | sort: 'date' %} <!-- Doesn't work -->
{% for resource in sortedResources %}
<div>
{{resource.title}}
<small>{{resource.date | date: "%d %b %Y"}}</small> <!-- Works -->
</div>
{% endfor %}
I'm using:
▶ ruby --version
ruby 2.1.4p265 (2014-10-27 revision 48166) [x86_64-linux]
▶ jekyll --version
jekyll 2.5.3
Thanks
If your Collection items have a valid date (ISO 8601 format) in the front matter they'll be sorted by date automatically, oldest first.
If you'd like to output more recent items first you can reverse the order like so:
{% assign sorted = site.resources | reverse %}
{% for item in sorted %}
<h1>{{ item.name }}</h1>
<p>{{ item.content }}</p>
{% endfor %}
I'm currently experiencing the same problem with collections.
While trying to sort on European formatted dates like dd/mm/yyyy or dd-mm-yyyy, I get a string sort. Even when the timezone: Europe/Paris is set in the _config.yml file.
The only way to get a collection sorted by date is to use ISO format yyyy-mm-dd.
# /_resources/example.md
---
title: Learn the Web
date: 2013-04-09
---
And the sort is now working.
Edit - This is how jekyll manages 'dates':
date: "2015-12-21" # String
date: 2015-12-1 # String D not zero paded
date: 01-12-2015 # String French format
date: 2015-12-01 # Date
date: 2015-12-21 12:21:22 # Time
date: 2015-12-21 12:21:22 +0100 # Time
If you don't need Time you can stick to the date: YYYY-MM-DD format.
And you have to be consistent across your collection. If you mix String, Date and/or Time Liquid will throw an error like Liquid error: comparison of Date with Time failed or Liquid error: comparison of String with Date failed
I got it: the resources where sorted by the date string (eg. 19-06-2015) which was not correct.
I created my custom filter instead:
# _plugins/filters.rb
module Jekyll
module DateFilter
require 'date'
def date_sort(collection)
collection.sort_by do |el|
Date.parse(el.data['date'], '%d-%m-%Y')
end
end
end
end
Liquid::Template.register_filter(Jekyll::DateFilter)
Used like so:
{% assign sortedResources = site.resources | date_sort | reverse %}
{% for resource in sortedResources %}
<div>{{resource.title}}</div>
{% endfor %}