StringResult+format doesn't seem to work in Webix datepicker - javascript

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.

Related

Moment returns an invalid date even when the format is passed

I have a string of the format "DD-MMMM-YYYY" ("18-April-2020"). So, In my app I create a moment object of the string using the code below,
moment(dob, "DD-MMMM-YYYY")
So the problem is when I get dob from my api, moment is failing to return a valid object.This is the object I get as shown in this image
But, If I got to momentjs website and go to their console and use the same string I got from api with the same format , I get a valid object from moment See this image

Sorting dates in DataTables? (function override)

DataTables properly sort dates in YYYY-MM-DD format only. All other formats they sort as string.
As far as I'm not a fan of adding a library / plugin for every functionality into my projects I tried to solve it by myself.
DataTables are using Date.parse() function to "understand" the date and sort it. (according to https://datatables.net/blog/2014-12-18).
So I decided to override that function and modify it in the way it will "understand" also other date formats.
I added this JS into my code:
let origFunction = Date.parse;
Date.parse = function(str) {
// I want to parse this date format: 27.01.2018
if (str.indexOf('.') > 0) {
str = convertDateToISO(str); // my function translates date into 2018-01-27
}
return origFunction(str);
};
When I test this in the browser console, Date.parse works perfectly fine with my date format, but DataTables keep sorting my european dates as strings.
Any idea what I'm doing wrong? Or is it possible to do it this way?
SOLVED:
After all it looks like really the easiest way is to include "Moment.js" plugin as described here: https://datatables.net/blog/2014-12-18#Operation
But while there was a "no plugin" requirement in my question, I'm accepting answer of #billynoah as a solution too.
You probably want to take a look at orthogonal-data. This allows you to define a custom value for sorting your column. In this case, the most straightforward thing to do is use a timestamp for sorting and then you can display date in any format you want. For data sourced from an object or ajax you can structure your column definition like:
{
data: 'date',
render: {
_: 'display',
sort: 'timestamp'
}
}
For html source you can use the data-order attribute:
<td data-order="1545466488">Sat, Dec 22nd 2018</td>
(Disclaimer / Attribution: This is all more or less straight out of the manual linked above)

Using same parsing format for output format

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.

jQuery.jgrid.parseDate date formatting

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.

date.format is not working

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();

Categories