With Hugo, I'm creating a blog in french.
Currently all my date are displayed in english (february) but I want them to be displayed in french (février).
How to set the language ?
My config.toml looks like this:
baseURL = 'http://example.org/'
languageCode = 'fr-FR'
defaultContentLanguage = "fr"
defaultContentLang = "fr"
title = 'TITLE'
theme = "THEME"
I try to set languageCode, defaultContentLanguage and defaultContentLang to fr but with no success.
I don't need multi language support, I just only need french.
To use localization, you need to use the Hugo function time.Format (which has an alias dateFormat). It takes two parameters:
desired format
time.Time object, or timestamp
Example:
{{ time.Format "Jan. 2, 2006" .Date }}
or
{{ dateFormat "Jan. 2, 2006" .Date }}
Docs: https://gohugo.io/functions/dateformat/
The .Format method (e.g. {{.Date.Format "Jan. 2, 2006}}) will not apply the desired localization.
Related
My problem is that the format FieldValue.serverTimestamp() sets is inconsistent. On 'add' it's a string and on 'update' it's a DateTime.
With:
FirebaseFirestore.instance.collection('collectionName').add(
{'lastEdited': FieldValue.serverTimestamp()},
)
It sets the date as (string):
With:
FirebaseFirestore.instance.collection('collectionName').doc('id').update(
{'lastEdited': FieldValue.serverTimestamp()},
)
It updates as a DateTime (this format would be preferable):
Because of this, some of the documents have a date that is string and some that is DateTime. This causes orderBy('lastEdited') to not work. Please do let me know if you have some insight to this behaviour. Thanks.
const date = DateTime.fromISO('2022-03-27T08:50').toFormat('H:mm a') // 08:50 AM
console.log(DateTime.fromISO(date))
If I attempt the above, in the console log I get this explanation in the 'invalid' field:
explanation: "the input "8:30 AM" can't be parsed as ISO 8601"
reason: "unparsable"
Is it not possible to revert the string back to a date?
You can parse back "8:30 AM" as DateTime using fromFormat:
Create a DateTime from an input string and format string. Defaults to en-US if no locale has been specified, regardless of the system's locale. For a table of tokens and their interpretations, see here.
but the information about year, month and day are lost and so the new DateTime will default to the current day.
Example:
const DateTime = luxon.DateTime;
const date = DateTime.fromISO('2022-03-27T08:50').toFormat('H:mm a') // 8:50 AM
console.log(DateTime.fromFormat(date, 'h:mm a').toISO())
<script src="https://cdn.jsdelivr.net/npm/luxon#2.3.1/build/global/luxon.min.js"></script>
How to show current date before clicking the date field in odoo?
Odoo Date field class provides methods to get default values for like today.
For dates the method is called context_today() and for datetimes context_timestamp(). You are able to pass a timestamp to this methods to either get today/now (without timestamp) or a timestamp which will be formed by the logged in users timezone.
Code Example:
from odoo import fields, models
class MyModel(models.Model):
_name = 'my.model'
def _default_my_date(self):
return fields.Date.context_today(self)
my_date = fields.Date(string='My Date', default=_default_my_date)
Or the lambda version:
my_date = fields.Date(
string='My Date', default=lambda s: fields.Date.context_today(s))
I found it.It is Simple, just write this on your python code like:
date = fields.Datetime(string="Date", default=lambda *a: datetime.now(),required=True)
or
like this
date = fields.Datetime(string="Date current action", default=lambda *a: datetime.now())
or
like this
date = fields.Date(default=fields.Date.today)
I am want to display a date in MM/yyyy format. I am using the below code to change the format :
var inputDate = new Date(data);
var date = dojo.date.locale.format(inputDate, {datePattern: "MM/yyyy", selector: "date"});
data contains the input date. For example when German Locale is set in the browser, the input value is like : 01.03.2016 05:30
while creating the date object with this value gives invalid Date though it works when the US locale is set in the browser.Please guide to fix this.
You can use locale.parse to convert your localized date string into a date object, then convert the date object into the formatted date you want.
See this small example:
var browserLocale = 'de',
data = '01.03.2016 05:30';
require(["dojo/i18n", "dojo/date/locale"], function(i18n, locale){
require([i18n.getL10nName("dojo/cldr", "gregorian", browserLocale)], function() {
var dateObject = locale.parse(data, {formatLength: 'short', locale: 'de'});
alert(locale.format(dateObject, {datePattern: "MM/yyyy", selector: "date"}));
});
});
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>
I have Laravel. I have a form. I have a MySQL database. There are some dates in it. When I bind the model and the form, the form is dutifully populated with raw MySQL dates. This is obviously not what I need.
My question is: How do I get a model bound form to display dates in a more readable way?!? There is not chance to intercept and format the data. Or maybe for a more general solution, is there any way to do some proessing on any data between the model and the form, before the user sees it?
Am I thinking of this all wrong? A billion thanks!
You may add an accessor method in your model like this:
public function getCreatedAtAttribute($date)
{
$date = new \Carbon\Carbon($date);
// Now modify and return the date
}
This will be called for created_at. Also check date-mutators if you need to override defaults. Check Carbon Docs.
You can format the date in the form using the format() method.
If you're using it in a form element:
{{ Form::text('name', $model->created_at->format('d/m/Y H:i')) }}
If you're displaying it as plain text:
{{ $model->created_at->format('d/m/Y H:i') }}
If you would like the user to be able to modify the date in a nice way, you could use three different select fields. Here's an excerpt from a project I worked on that allows the user to change the date of birth:
<div class="form-group {{ $errors->has('date_of_birth') ? 'has-error' : '' }}">
{{ Form::label('date_of_birth', 'Date of Birth', ['class' => 'control-label']) }}
<div class="form-inline">
{{ Form::selectRange('date_of_birth[day]', 1, 31, null, ['class' => 'form-control']) }}
{{ Form::selectMonth('date_of_birth[month]', null, ['class' => 'form-control']) }}
{{ Form::selectYear('date_of_birth[year]', date('Y') - 3, date('Y') - 16, null, ['class' => 'form-control']) }}
</div>
{{ $errors->first('date_of_birth', '<span class="help-block">:message</span>') }}
</div>
This is contained within a form that has the model bound to it.
Side Note
The alternative answer posted in here actually overrides the default way that laravel handles dates, and unless you actually return a carbon object, you'll never have the luxury of using the format method, or any of the other handy things that carbon has.
If you have other columns that contain dates, add them to the data mutators list:
public function getDates()
{
return ['created_at', 'updated_at', 'date_of_birth', 'some_date_column'];
}
This will now make it so that each of those is an instance of Carbon allowing you to format as you please, whenever you please and easily modify, duplicate and a whole host of other things. For more information regarding this, see: http://laravel.com/docs/eloquent#date-mutators
You can format all fields retrieved from the database with creating an Accessor in your model. For example if your database field is created_at use getCreatedAtAttribute.
public function getCreatedAtAttribute($date)
{
return Carbon::($date)->format('d/m/Y');
}