jqgrid datetime formatting - javascript

I have a spring MVC java application and I'm serializing joda DateTime to json.
When I examine the output through the browser the DateTime serialized data looks like this:
startDate: 1323147660000
I'm not sure which format this data is in. I've tried many different combinations of srcformat and newformat format options including the following based on this post:
{srcformat:'U', newformat:'m/d/Y'}
My hunch is that this is the number of milliseconds since the epoch but I'm not sure how to use it correctly within jqgrid.
Thanks in advance for any help.

Actually the milliseconds from epoch format was supported out-of-the-box in one of the previous versions of jqGrid. Unfortunately it has been dropped for an unknown reason.
Here is a workaround:
{
name:'startDate',
label: 'Start date'
formatter: function(cellValue, options) {
if(cellValue) {
return $.fmatter.util.DateFormat(
'',
new Date(+cellValue),
'UniversalSortableDateTime',
$.extend({}, $.jgrid.formatter.date, options)
);
} else {
return '';
}
}
}
Note that with custom formatter you can parse the date and format it in any way you wish. However I did my best to use built-in jqGrid formatting facilities (see the UniversalSortableDateTime?)

Related

How to change timestamp format of wikipedia page's last modified section

From various API:Client code I am using mediawiki-js
to get last modified date of Wikipedia pages. While referring MediaWiki API help I find various formats for Timestamp but I am unable find syntax to use it. The following code :
var mwjs = new MediaWikiJS('https://en.wikipedia.org', { action: 'query', prop: 'revisions', titles: 'Main_Page' }, function (data) {
'use strict';
var pages = data.query.pages;
alert('Main page of Wikipedia last edited by: ' + pages[Object.keys(pages)[0]].revisions[0].timestamp);
});
gives default format. How can I change it to any other required format?
As far as I can tell, the various date formats you linked to are for input that you can specify to the API, not the output that you get from the API.
If you want to show the date in a different format to the user, you should use the date parsing and formatting functions of the language you use.

Angular.js date parsing

I'm working on a project in which I need to implement a list of dates. I have been able to pull the data from the api, however I have been having troubles parsing the format within these dates.
The data that I have been able to pull looks similar to this
[["2015-10-05T13:00:00Z","2015-10-05T21:00:00Z"],
["2015-10-06T13:00:00Z","2015-10-06T21:00:00Z"],
["2015-10-07T13:00:00Z","2015-10-07T21:00:00Z"]]
Which is sweet that i've been able to pull.....but as you can see, the dates are not really what a user will need.
I've been looking into moment() methods. (http://momentjs.com/) However the problems that i've run into is that the methods that you can use with moment() is that i've only been able to make anything work with one date, not with an array of dates such as what i have.
So my question is, are there any alternatives to moment(), or better ways of parsing an array of dates?
You'd have to loop and parse.. you can do it fairly simply with a .map call (with momentjs):
var formattedDates = array.map(function(inner) {
return inner.map(function(d) {
return moment(d).format("MM/DD/YYYY hh:mm A");
});
}).reduce(function(p, c) {
return p.concat(c);
});
Demo: http://jsfiddle.net/6ncpspc0/

What format for dates does jQuery.ajax() expect JSON datatype

Strange that I can't find an answer to this one...
Simply, what date format (if any) will a call using jQuery.ajax() deserialize properties into javascript dates?
ISO 8601 doesn't seem to work. The following: 2015-10-21T23:44:21.292Z and 2015-10-21T23:44:21Z both fail.
$.ajax({
url: '/trees/leopardTree-01',
dataType: 'json',
success: function (tree) {
if (tree.PlantingDate instanceof Date) {
//YAY I know when my tree was planted
}
}
});
I want to know when my tree was planted.
Update on duplicate
It's not quite the same question as The “right” JSON date format. Although that question is informative, if a little philisophical (e.g. what is the right colour for a flower to be?). I'm interested in a solution to automatically parse a string into a datetime object, using a framework, in this case jQuery. This could be via a custom formatter as per #Neverever's comment (e.g. pick me only blue flowers). I'm surprised jQuery doesn't offer this sort of thing as an option.
JSON itself does not have a standard representation for datetime. This implies that Ajax can not parse dates automatically, you have to do it yourself:
Example (when tree.PlantingDate in ISO 8601 format):
success: function (tree) {
var plantingDate = new Date(tree.PlantingDate);
if (!isNaN(plantingDate)) {
//YAY I know when my tree was planted
}
}

How to deal with local/UTC DateTime conversion in Kendo MVC Grid?

I'm getting mad with the Kendo grid. In my ASP.NET MVC project I use a database that contains archive values with a UTC datetime. I want to show these values on my page, but I need the datetime values to be shown as local (browser context) datetime. I also do have two JQuery datepickers for the start/end datetime selection of the desired range that must also work with local time. These two datepickers modify the filters of the grid on each change.
I already managed to change the filter's datetime to UTC by using this code when the datepicker's values change:
var filterExists = false;
$.each($filter, function (index, entry) {
if (entry.field == "DateValue" && entry.operator == "lte") {
entry.value = EndDate.toISOString();
filterExists = true;
console.log("UTCEndDate: " + EndDate.toISOString());
}
})
if (filterExists == false) {
$filter.push({ field: "DateValue", operator: "lte", value: EndDate.toISOString() });
}
When I look into the POST request, the start and end datetimes get send as UTC.
Now the controller fetches the data from the DB like this:
public ActionResult JournalData([DataSourceRequest]DataSourceRequest request)
{
JsonResult jsonNetResult = new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet };
var JournalData = db.DoubleDataValueArchive.ToDataSourceResult(request, x => new JournalEventModel
{
DateValue = x.DateValue,
StationId = x.StationId,
Value = x.Value,
});
jsonNetResult.Data = JournalData;
return jsonNetResult;
}
Using this code, the data gets returned to the grid in UTC. The data is shown in the grid, but the datetimes don't match the values in the datepickers (which show the local datetime).
As I could not figure out a way to convert these datetimes to local time in the grid itself, I tried converting it to local time in the controller by using
DateValue = x.DateValue.ToLocalTime(),
in the code shown before. But now the grid's datetime filter values don't match. Now, when I want to show data for e.g. the last two hours, the most recent hour is not shown when the client's time is UTC+1. This is extremely annoying and I don't know how to resolve this issue.
As far as I can see, sending and returning datetimes to and from the server in UTC would be the most stable way. All conversion should be done on client side and not on the server. It seems that the grid can not work with UTC "under the hood" and display local time whereever it gets rendered on the page.
I hope that somebody ran into this issue before. Thanks for your help!

OData Date Filtering from JS

I am using the DXTREME framework from Devexpress to connect a HTML mobile app to an OData source.
One of my tables in SQL Server, exposed through the OData service is a table with a date (not datetime) field in it. It is exposed through OData like this:
<d:TaskDate m:type="Edm.DateTime">2010-04-01T00:00:00</d:TaskDate>
I am trying to filter the data on this field through a calendar control, but when I try to filter the datasource on the JS side, I get no matches. This is because the date is passed to the OData service, I believe, in UTC format, so if I query for TaskDate = '10/JUL/2013', I believe the date is passed as "09/JUL/2013 14:00". If I filter on TaskDate > '10/JUL/2013' I get results back from after "09/JUL/2013 14:00" at any rate.
I have tried declaring a new date with no time part:
filterDate = new Date(2013, 6, 10)
but is still doesn't work, it still subtracts 10 formy time zone on the JS side.
What I want to do is to return a lists of Tasks valid on that particular date. How can I achieve this?
I think my problem was the confusion around the dxDateBox control returning just a date, and that date being changed when passed to my odata service.
I solved the issue by converting the date to UTC myself, but just using the Date parts from the control, (where filterDate came from the control):
var paramDate = new Date(Date.UTC(this.filterDate().getFullYear(), this.filterDate().getMonth(), this.filterDate().getDate()));
this.dataSource.filter(["TaskDate", "=", paramDate]);
This works nicely, but seems rather verbose.

Categories

Resources