How to put a / in vaadin-date-picker - polymer

I am using a vaadin-date-picker with polymer. I would like to put the right slash by default when the user types in a date in US format. So if the user types 01152019, it should auto format the date to 01/15/2019. Right now if I enter date without the / , it wont take recognize the value.

You should set the parseDate method of the i18n property of your date picker so that it does what you want, for example, if you only wanted to accept date strings in US format but without slashes or hyphens you could do something like this:
const datePicker = something; // you probably did something like this.querySelector() to get it
datePicker.i18n = {
// you can also set methods to format the date or the title and a few other things
parseDate: dateString => {
let month = parseInt(dateString.subString(0,2));
let day = parseInt(dateString.subString(2,4));
let year = parseInt(dateString.subString(4,8));
return {day, month, year};
}
};

Related

Highcharts with external CSV $.get - No xAxis date

I'm trying to create a spline chart using this CSV:
slave_id,date,time,rtc_temp,temp1,temp2,temp3
1,2017/12/26,16:42:59,21,11.50,13.13,5.88
2,2017/12/26,16:43:29,21,14.13,20.63,99.99
1,2017/12/26,16:44:00,21,11.50,13.13,5.88
2,2017/12/26,16:44:30,21,14.13,20.63,99.99
1,2017/12/26,16:45:01,21,11.50,13.13,5.88
2,2017/12/26,16:45:31,21,14.13,20.63,99.99
1,2017/12/26,16:46:02,21,11.50,13.13,5.88
2,2017/12/26,16:46:32,21,14.13,20.63,99.99
As you can see here [IMAGE], the graph is showing the date and time, but the x Axis is not accepting the date / time.
Ive tried using date.UTC, but that did not work either. Can someone point me in the right direction?
https://jsfiddle.net/asvoy6b9/ [not working due to CSV missing]
Full code [Hastebin]
I see that date variable in your code is a string:
// all data lines start with a double quote
line = line.split(',');
date = line[1] + " " + line[2];
(...)
RTC.push([
date,
parseInt(line[3], 10)
]);
If you choose to construct the point's options as an array of two values and the first value is a string then it's treated as its name property (not x).
Explanation: https://www.highcharts.com/docs/chart-concepts/series
In that case Highcharts assigns subsequent integers as x values for all points (that's why there're values like 00:00:00.000 (1 Jan 1970), 00:00:00.001 etc.).
You need to parse your date to timestamp. You can use Date.UTC() (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC) or some other function for this.
I've managed to get it working with Date.UTC using the following code:
var yyyymmdd = line[2].split("-"); //Split the date: 2017 12 16
var hhmmss = line[3].split(":"); //Split the time: 16 11 14
var date = Date.UTC(yyyymmdd[0], yyyymmdd[1] - 1, yyyymmdd[2], hhmmss[0], hhmmss[1], hhmmss[2]); //Stitch 'em together using Date.UTC

Extjs date format issue safari firefox

Dates are stored in the format, YYYY-mm-dd hh:mm:ss in the database.
e.g. 2014-07-03 00:00:00
But I want my Ext.Field.Date to have the format:
Y-m-d H:i
As per my component set up:
xtype: 'datefield',
id: 'p_p_start',
fieldLabel: 'Planned Start',
name: 'plannedstart',
allowBlank: false,
vtypeText: 'Date not valid',
format: 'Y-m-d H:i',
It is quite reasonable of me to want to be able to display the date in browsers other than just Chrome. To-date, FireFox and Safari will display nothing.
I've looked at a similar issue on SO which proposed support of all browser by splitting and reassembling the string format in a variation of:
var c=Ext.getCmp('p_p_start');
var st_date = "2014-05-08";// with alt formats changed Y-m-d, leaving time out diags
var dateParts = st_date.split(/-/);
var d = new Date(dateParts[0],parseInt( dateParts[1], 10) -1,dateParts[2]);
console.log("Date parts= " + dateParts);//Date parts= 2014,05,08
c.setValue( d ); //NOPE!
I was very happy with progress in Chrome (no problems at all) but have yet to succeed in getting anything working in FF or Safari (not tried IE but it has to work in that also). So, in summary, the question is,
How do I get date formats in all browsers to work with the format Y-m-d H:i using setValue?
Many thanks in advance.
Kevin
A date field's internal value is a JavaScript Date Object, which is not tied to a specific format. The format config simply defines the display and input format of the field.
You can use setValue with a string, but only if the string already conforms to your defined format.
In your case the problem seems to be converting a date string value with a different format which was received from the server/database to a Date object.
Have a look at the Ext.Date singleton, specifically its function Ext.Date.parse, to achieve that:
var st_date = "2014-05-08";
var dateObj = Ext.Date.parse(st_date, "Y-m-d");
Ext.getCmp('p_p_start').setValue(dateObj);
var st_date2 = "2014-07-03 00:00:00";
var dateObj2 = Ext.Date.parse(st_date2, "Y-m-d H:i:s");
Ext.getCmp('p_p_start').setValue(dateObj2);

Converting Phonegap app datetime fields to work on ios7

So I just found out that the datetime input fields I use all over my app are no longer working on IOS7, so I found that the datetime-local input type is still supported. Timezone is not important to the function of my app, so this is ok. The problem is, the code I was using to populate and retrieve the date values from the input fields does not work. Here is my code:
$("#date").val(pv.When);
To set it, where date is the id of my input and pv.When is a datetime object
theVisit.When = new Date($("#date").val());
To retrieve it, right now neither do anything, the field is empty when i load the form, and the value does not save. Do I need to do anything special to make this work?
OK, what I did was this, I wrote 2 utility functions, one to set the value of the datetime-local field and one to get the value out as a date. You will notice as part of the fix I am using the Moment.js date helper library.
Here are my 2 functions:
function getDateTimeForPicker(d) {
var offset = d.getTimezoneOffset() / 60;
d.setMinutes(d.getMinutes() - d.getTimezoneOffset())
return d.toISOString().replace("Z", "");
}
function getDateTimeFromPicker(d) {
var m = moment(d);
var ret = m.toDate();
return ret;
}
so I can set as easy as :
$("#dateTimeBox").val(getDateTimeForPicker(new Date()));
This is tested and working fine on iOS7

Disable certain dates from html5 datepicker

Is it possible to disable dates when I use
I want to disable current date for one scenario and future dates for other scenario.
How should I disable the dates?
You can add a min or max attribute to the input type=date. The date must be in ISO format (yyyy-mm-dd). This is supported in many mobile browsers and current versions of Chrome, although users can manually enter an invalid date without using the datepicker.
<input name="somedate" type="date" min="2013-12-25">
The min and max attributes must be a full date; there's no way to specify "today" or "+0". To do that, you'll need to use JavaScript or a server-side language:
var today = new Date().toISOString().split('T')[0];
document.getElementsByName("somedate")[0].setAttribute('min', today);
http://jsfiddle.net/mblase75/kz7d2/
Ruling out only today, while allowing past or future dates, is not an option with here. However, if you meant you want tomorrow to be the min date (blanking out today and all past dates), see this question to increment today by one day.
As in all other cases involving HTML forms, you should always validate the field server-side regardless of how you constrain it client-side.
In pure HTML, the only restrictions you can put on dates are its lower and upper bounds through the min and max attributes. In the example below, only the dates of the week I'm posting this question are allowed, other appear greyed out and clicking on them doesn't update the input value:
<input type="date" min="2019-06-02" max="2019-06-08"/>
You can also disable any invalid date by using a few lines of JavaScript, but this doesn't ship with all the native <input type="date"> features like greyed-out dates. What you can do is set the date value to '' in case of an invalid date, an error message could also be displayed. Here is an example of an input that doesn't accept weekend dates:
// Everything except weekend days
const validate = dateString => {
const day = (new Date(dateString)).getDay();
if (day==0 || day==6) {
return false;
}
return true;
}
// Sets the value to '' in case of an invalid date
document.querySelector('input').onchange = evt => {
if (!validate(evt.target.value)) {
evt.target.value = '';
}
}
<input type="date"/>
HTML datepicker (<input type=date>) supports min/max attribute, but it is not widely supported.
At the meantime you may consider using bootstrap-datepicker, v1.2.0 is on github.
References:
W3C spec
You could use this to disable future dates :
Inside you document.ready function, place
//Display Only Date till today //
var dtToday = new Date();
var month = dtToday.getMonth() + 1; // getMonth() is zero-based
var day = dtToday.getDate();
var year = dtToday.getFullYear();
if(month < 10)
month = '0' + month.toString();
if(day < 10)
day = '0' + day.toString();
var maxDate = year + '-' + month + '-' + day;
$('#dateID').attr('max', maxDate);
and in form
<input id="dateID" type="date"/>
Here is the working jFiddle Demo
For react and similar libraries, you may use this to disable all dates before today.
<input type='date' min={new Date().toISOString().split('T')[0]} >
Depending on what you need, you can also use the step attribute to only enable specific dates - e.g. every Monday, or every other day. You can use it in combination with min and max
e.g. every Monday
<input type="date" step="7" value="2022-04-04">
Every Thursday
<input type="date" step="7" value="2022-04-07">
Every other day
<input type="date" step="2">

Select statement selection through URL parameters

I'm attempting to alter the contents of certain parts of a HTML form through usage of the URL. For a text field, I'm aware that this will suffice,
http://<domain>?fieldname=ping&anotherfield=pong
On the form there are multiple select braces (drop down boxes); Is it possible to pick an int or string value through the url for this?
There seems to be little documentation on this (or even people trying to do the same)...
You haven't specified how you want to do this, but I'll assume that you want to use JavaScript:
To get a value from QueryString:
getQueryStringArgument = function(key) {
var hu = window.location.search.substring(1);
var gy = hu.split("&");
for (i = 0; i < gy.length; i++) {
var ft = gy[i].split("=");
if (ft[0] == key)
return ft[1];
}
}
To set the selected value of the select list:
document.getElementById("sel").value = getQueryStringArgument("id");
For a text field, I'm aware that this will suffice
No, it won't (at least, not in a generic way).
For a text field, the default value is specified by the value attribute. There might be a server side script that populates it based on query string data, but there doesn't have to be.
On the form there are multiple select braces (drop down boxes); Is it possible to pick an int or string value through the url for this?
Again, this requires an attribute to be set (selected on <option>), and that could (again) be set by a server side script based on the query string data.