JQuery Mobile/Datebox, difference between display date format and submit date format - forms

Is there a way to display a date in an input different from the format I want to submit.
For example I have to submit a date in "yyyy-mm-dd" format but I want to display the date in another format : "dd/mm/yyyy" (french display).
Is there a good tip to do that with Datebox for jQuery Mobile (an option I didn't see ?)
I think I have to cheat in creating an input hidden with the good form format and another one with the format to display (and not submitted), but maybe a better solution exists.
Any ideas ?

Your best bet is to indeed use 2 inputs - but, it's pretty easy to do, and using a callback on the set event, you can even make datebox do the second format for you.
http://dev.jtsage.com/jQM-DateBox2/demos/script/split.html
(Note: I just added the demo, so you didn't miss it earlier)

Just a small addition
It should be "overrideDateFormat":"%d/%m/%Y" in the HTML inline options.
In http://dev.jtsage.com/jQM-DateBox2/demos/script/split.html it states "dateFormatOverride":"%d/%m/%Y"} however this is incorrect and doesn't work. Just a heads up for anyone else with this issue.

Yes you need to use this method.
<!-- fix american date formatting -->
<script type="text/javascript">
jQuery.extend(jQuery.mobile.datebox.prototype.options, {
'overrideDateFormat': '%d/%m/%Y',
'overrideHeaderFormat': '%d/%m/%Y'
});
</script>

Related

CakePHP 4 Date format for FormHelper->date() how to change to UK format

I have the following code in a template:
<?= $this->Form->date('selected_date', ['required' => true]) ?>
This displays a lovely new dynamic date picker, but with the US format "mm/dd/yyyy". What I want is "dd/mm/YYYY"
In app.php I've set the APP_DEFAULT_LOCALE to en-GB.
In AppController.php I've set the following:
I18n::setLocale('en-GB');
Time::setDefaultLocale('en-GB'); // For any mutable DateTime
FrozenTime::setDefaultLocale('en-GB'); // For any immutable DateTime
Date::setDefaultLocale('en-GB'); // For any mutable Date
FrozenDate::setDefaultLocale('en-GB'); // For any immutable Date
How do I change the format? I can't find anything in the docs or online.
You can't change it, at least there's no reliable, cross browser/device compatible way, the control is rendered by the browser, and the current state of things is that browsers use the locale the browser currently runs in to format the control.
If you want something solid, then you'll have to use a custom JavaScript datepicker. If you want to walk on the edge, look into web components.
See also Is there any way to change input type="date" format?.

Automatically set date in the front matter of a Jekyll page

When I create a post, in the front matter I have to insert layout, title, date etc. My concern is regarding date front matter. It is very inconvenient to insert date time and time offset in date field manually.
I'm not able to figure out how to do this automatically. I want the front matter date to update automatically with system time.
The date variable in front matter is an optional variable that you can set to be able to override the date that you set when you title your post according what Jekyll is looking for (i.e. 2019-12-14-post-title.md). It should only refer to the date the post was originally made, and not the current system date. Except for certain edge cases, you really shouldn't have to define date in the front matter.
You can use Liquid templating to post the current time to a page by passing the "now" keyword to date in your liquid template as follows:
This page was last updated at {{ "now" | date: "%Y-%m-%d %H:%M" }}.
to get the the output:
This page was last updated at 2019-12-14 17:48.
However this method has a big drawback for Jekyll usage, because it will refer to the current date when the page was last generated from the template and not when the page is being visited by the user.
The best way to access the current system time is likely to use javascript instead. This is fairly easy depending on your familiarity with the language, but essentially you can make a Date object and then output the info you are looking to display. Here is a quick snippet I made that shows a couple methods that act on the Date object to output different values such as the date, time, and year:
<!DOCTYPE html>
<html>
<body>
<h3>The toDateString() method converts a date to a date string:</h3>
<p id="demo1"></p>
<h3>The toLocaleTimeString() method outputs formatted time:</h3>
<p id="demo2"></p>
<h3>The getFullYear() method outputs the year:</h3>
<p id="demo3"></p>
<script>
// create date object
var d = new Date();
// use toDateString() to output formatted date
document.getElementById("demo1").innerHTML = d.toDateString();
// use toLocaleTimeString() to output formatted time
document.getElementById("demo2").innerHTML = d.toLocaleTimeString();
// use getFullYear() to output year
document.getElementById("demo3").innerHTML = d.getFullYear();
</script>
</body>
</html>
W3schools has a good rundown of the Date object and how to format output if you need more examples and reference on usage.
If you need to know how to use Javascript with Jekyll there is a helpful post on Jekyll Talk that will help.
First of all, you do not HAVE to insert 'layout, title, date etc'. You can use Front Matter defaults for them. This only makes sense for the layout as the title can or should be default, nor the date.
The date MUST be set in your filename and can be overridden in the Front Matter. Please note that this is ONLY true for the built-in collection posts. If you use a custom collection you do not need a date at all.
However, if you choose to use posts nevertheless and you want to add the date automatically, there is only one real option: Use a CMS that automates this input, like Forestry.io or CloudCannon.

eonasdan datetimepicker century threshold

Any tips would be welcome: I need the datetimepicker to transform a date earlier than 1969 to that year in the 20th century. So if I enter 12-12-69 the datetimepicker transforms that into 12-12-1969. Entering 12-12-68 though is transformed into 12-12-2068. I assume this is done in moment.js, but I may be mistaken. If it is done in moment.js anyone have an idea where to look to change the threshhold for this automagic transformation? I'd rather change it in the library than use parseInputDate, but if anyone has an example on usage of parseInputDate, please share. I have found that the date already has been changed automagically as ddescribed above when I try and access the date-object via:
$('#datefrom').on('dp.change',function(e) { console.log(e.date); });
I need to access the date object B4 this happens. It works to set a onchange="dostuff()" inline on the input field itself, but that sort of feels wrong, as I want to handle this in one place in the datetimepicker and not in 50 places inline.

reformat date that is pulled from database table

I have a website which uses smarty templates.
I have a table in my db called posts that has various columns, one being "date_added". I managed to have that displayed on the posts by editing one of the Smarty templates for "posts" however, the date format is YYYY-MM-DD.
Is there any easy way for me to change this? Perhaps with jQuery?
Ideally, I want to only show the abbreviated month, with the day positioned next it. This is for a blog style post, but this isn't WordPress.
Right now the smartytemplate the shows the date_added reads like this:
{$posts[i].date_added|stripslashes|nl2br}
Where posts is the table and date_added is a column in that table.
An exact example can be seen here in the top right corner of each post.
http://www.elegantthemes.com/preview/LightBright/
Does anyone have a good suggestion of how I can achieve the desired request?
If you are looking for a javascript solution, you can take a look at the incredible JS Library MomentJS. It is very lightweight and does numerous date and time formats.
http://momentjs.com/
Just include the minified script file in your HTML .
For your exact case, you would use momentJS as such:
First create the momentJS date object:
var moment_date = moment(date_from_database, "YYYY-MM-DD");
Then to display the date as you want:
var date_string = moment_date.format("MMM DD"); // ex. = "Mar 03"
More documentation here: http://momentjs.com/docs/#/displaying/
Then you can use your DOM manipulation library (JQuery for example) to place that string somewhere in your HTML
Good luck
- K

jquery datepicker date format with output of Zend_Locale

I'm trying to set the format for a jquery datepicker element with the date format returned by Zend_Locale::getTranslationList('date', $locale);
My problem is zend returns the string 'dd/MM/yyyy' for the date format but jquery expects only 2 characters for the year ie 'dd/mm/yy', so it enters the year twice 20112011
Is there some option that can be passed to either zend or jquery to make them work in the same manner? I've read through the docs and can't seem to find anything
Many thanks for your help in advance!
I have used jquery date picker in python-django framework when I give date format as dd/mm/yyyy format it returns 20112011 then i have corrected it to dd/mm/yy and it returned 2011 only. you can specify in your datepicker css class to specify the date format as dd/mm/yy.
Iam not sure if this is what you're looking for but:
// use german local, change it to our needs :-)
$locale = new Zend_Locale('de_DE');
$result = Zend_Locale::getTranslationList('date', $locale);
// returns dd.MM.yy (german!)
echo $result['short'];