I am trying to use the date.format and getting an error that format is not a recognized method. I tried to use thesame in new solution, and I'm getting a correct result. Any idea why .format method is not working? These both are in Javascript.
Assuming you are talking about Date type...
It is clear why format method is not working - because there is no such method on Date object.
It is less clear why/when it would work - most likely you are using some script library that modifies Date object to have format method.
But if you are talking about some other type - format function may be present (or added) to any object and it is not possible to suggest anything without code...
I.e. custom object with format function:
var date = { format:function() {alert("Hi");}};
date.format();
Related
I expect that stringResult will give me an output that corresponds to the specified format, but it always looks like YYYY-MM-DD HH:MM
Full code here.
For example, I've tried the following config:
{
view:"datepicker",
stringResult:true,
format:"%Y-%F-%d"
},
But the output is still the same. So, is it a bug? Or am I doing something wrong? Thank you in advance!
[Updated]
As mentioned by Loj, I agree with it. Thus, there are 2 solutions possible:
1. Custom Format
The format property you have used sets a date format to display in the datepicker field. So, it is just the display format and it is not actually formatted. Hence, the stringResult returns date as string with the default format.
In order to get the custom date in the output, you are required to add your custom format which will convert the date in the desired format.
var format = webix.Date.dateToStr("%Y-%F-%d");
Check the snippet here.
2. Using getText()
Using stringResult property in the control's configuration makes the getValue method to return raw unformatted value. Hence, instead you should use the getText() as
$$("custom").getText();
in your code to get the formatted output via stringResult.
With libary moment there is option to bring a array of formating options and momentjs use the best match for parsing the input.
For Example:
var date = moment("29-06-1995", ["MM-DD-YYYY", "DD-MM", "DD-MM-YYYY"]);
but what if I want the take the same format that using in parsing for output formating.
var dateText = date.format('selected parse')
How do I know which format moment choose to use?
Currently there is no exposed function for getting the chosen format, however there is a "private" field named _f that contains this information.
var m = moment("29-06-1995", ["MM-DD-YYYY", "DD-MM", "DD-MM-YYYY"]);
m._f // "DD-MM-YYYY"
If you use this, be careful when updating versions of moment. Private fields are not guaranteed to be maintained, and could break between versions.
I've logged this as a feature request for future moment.js functionality.
I have searched for this but couldn't find anyone trying to do what i am doing with jqGrid formatters.
I have a date which I am trying to parse which is not in a jqGrid table, but I am using jqGrid on the site elsewhere and am hoping to parse this date using jqGrid's date parser instead of having to write a seperate method.
I have been messing around with the jGrid object but I need a little help with this.
My date format is ISO8601Long i.e. 2013-11-17T09:00:00
First, I am ensuring the date formats in the formatter are the ones i wish to use:
jQuery.jgrid.formatter.date.srcformat = 'Y-m-d H:i:s';
jQuery.jgrid.formatter.date.newformat = 'j/m/Y g.ia';
Next, I call the jQuery.jgrid.parseDate method in an attempt to parse the date.
jQuery.jgrid.parseDate(0, 0, '2013-11-17T09:00:00');
I am not sure what the first two arguments are, but they look like they might accept a srcformat and newformat. Either way, they don't format as I would expect. There is a fourth too, but this seems to only accept an object.
This leaves me with 2013-11-17GMT09:00:00. While this is a bit easier to understand, it is not in the format I would be expecting. I presume I have missed a step or somehow need to call the formatter after this.
Any pointers would be great.
Thanks,
Dale
The correct usage of parseDate would be the following:
var parsedData = $.jgrid.parseDate("Y-m-d H:i:s", "2013-11-17T09:00:00", "j/m/Y g.ia");
or
var parsedData = $.jgrid.parseDate("ISO8601Long", "2013-11-17T09:00:00", "j/m/Y g.ia");
You can use alternatively Globalize jQuery Plugin.
We've got a problem where jquery is giving us an "E.split is not a function" error when we try to attach a date as data to a DOM object.
We are creating our date as follows:
new_end_date = new Date(start_time_date);
new_end_date.setMinutes(start_time_date.getMinutes() + service_duration);
Then we are using the .data() function to attach the data to a dive with the id end_time as follows
$("#end_time").data(new_end_date);
According to our reading the .data() function should be able to "attach data of any type to DOM elements" (see: http://api.jquery.com/data)
However it causes the split is not a function error.
It works fine if we replace the date reference with a string so it appears to be related to Jquery's handling of the date object.
Thanks for any assistance you can offer.
The data function expects a key.
$("#end_time").data("enddate", new_end_date);
or an object (with key/value pairs)
$("#end_time").data({ enddate: new_end_date });
Your using .data wrong. your supposed to store the data under a key. Like
$("#end_time").data("end-date", new_end_date);
Then you can call $("#end_time").data("end-date") to get that date out again.
jQuery.param({foo: 1}); // => "foo=1" - SUCCESS!
jQuery.param({bar: new Date()}); // => "" - OUCH!
There is no problem with encodeURIComponent(new Date()), which is what I would have thought param is calling for each member.
Also, explicitly using "traditional" param (e.g. jQuery.param(xxx, true)) DOES serialize the date, but alas, that isn't of much help since my data structure isn't flat.
Is this because typeof(Date) == "object" and param tries to descend into it to find scalar values?
How might one realistically serialize an object that happens to have Date's in it for $.post() etc.?
You're probably going to want the date transformed into a string, since that's what it's going to have to be on the wire anyway.
$.param({bar: new Date().toString()});
Now you may want it formatted in some particular way so that your server gets something it can parse. I think that the datejs library has support for formatting, or you could roll your own by picking out pieces of the date with getDate(), getMonth(), getYear() etc.
If you work with Microsoft products on the server side you should take in consideration, that Microsoft serialize Date as a number of milliseconds since UTC, so as a number. To be more exact, the serialization string look like /Date(utcDate)/, where utcDate date is this number. Because JSON supports the backslash as an escape character you should use code like following to serialize a Date object myDate:
"\/Date(" + Date.UTC(myDate.getUTCFullYear(), myDate.getUTCMonth(),
myDate.getUTCDate(), myDate.getUTCHours(),
myDate.getUTCMinutes(), myDate.getUTCSeconds(),
myDate.getUTCMilliseconds()) + ")\/"
I think this is a jQuery bug in the following context:
jQuery 1.4.2 (1.3.2 works)
new methods added into Date.prototype