Objective: To export data displayed in jqGrid as CSV maintaining existing formatting. Make a generic utility to be used across multiple webpages using jqGrid for exporting data.
Thanks to excellent questions posted for exporting data before, I am able to create a csv formatted data from jqgrid and pass it to backend to be saved as .csv file.
Steps Taken:
Used jqGrid('getGridParam', 'data') to get all row data
Used jqGrid('getGridParam', 'colNames') to get colnames
Created a tab separated output- No issues.
Problem: Since I used 'data', the column values are the raw values and not the formatted values.
For eg, from backend the date comes as a long -1411674947000 but using custom formatter in jqgrid it displays as 2014-09-25 19:55:47.
Similarly there are error codes which come as numeric values but formatted to show some text.
The objective is to use the formatted value eg '2014-09-25 19:55:47' in the csv output instead of '1411674947000'.
Not sure exactly how the data becomes a CSV - but before you export the data, you could use Javascript's toUTCString() function to convert the timestamps into human-readable strings:
var oldDate = new Date(1411674947000);
var newDate = oldDate.toUTCString();
console.log(newDate); // Thu, 25 Sep 2014 19:55:47 GMT
Here's a JSFiddle.
Related
After googling around, I cannot find a way to create a new table with a DATETIME column with the default format set to 'DD-MM-YYYY HH:MM:SS'
I saw a tutorial in which it was done in phpmyadmin so I suspect that I could use mysql via command line and achieve the same thing when creating my new table with
CREATE TABLE ()
Thank you in advance
"MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format."
This is from mysql site. You can store only this type, but you can use one of the many time format functions to change it, when you need to display it.
Mysql Time and Date functions
For example, one of those functions is the DATE_FORMAT, which can be used like so:
SELECT DATE_FORMAT(column_name, '%m/%d/%Y %H:%i') FROM tablename
Use DATE_FORMAT function to change the format.
SELECT DATE_FORMAT(CURDATE(), '%d/%m/%Y')
SELECT DATE_FORMAT(column_name, '%d/%m/%Y') FROM tablename
Refer DOC for more details
As others have explained that it is not possible, but here's alternative solution, it requires a little tuning, but it works like datetime column.
I started to think, how I could make formatting possible. I got an idea. What about making trigger for it? I mean, adding column with type char, and then updating that column using a MySQL trigger. And that worked! I made some research related to triggers, and finally come up with these queries:
CREATE TRIGGER timestampper BEFORE INSERT ON table
FOR EACH
ROW SET NEW.timestamp = DATE_FORMAT(NOW(), '%d-%m-%Y %H:%i:%s');
CREATE TRIGGER timestampper BEFORE UPDATE ON table
FOR EACH
ROW SET NEW.timestamp = DATE_FORMAT(NOW(), '%d-%m-%Y %H:%i:%s');
You can't use TIMESTAMP or DATETIME as a column type, because these have their own format, and they update automatically.
So, here's your alternative timestamp or datetime alternative! Hope this helped, at least I'm glad that I got this working.
i have used following line of code & it works fine Thanks.... #Mithun Sasidharan
**
SELECT DATE_FORMAT(column_name, '%d/%m/%Y') FROM tablename
**
I'm pretty certain that you can't change the datetime format in mysql. The phpmyadmin setting is probably applying a custom format as it reads the datetime (using DATE_FORMAT or something from php). It shouldn't matter what format the database uses, format in the application to display it as you wish.
Date formatting is a pretty common task. I typically like to abstract it out into internationalization code or, if you don't need to deal with i18n, into a common date utility library. It helps keep things consistent and makes it easier to change later (or add i18n support).
No you can't; datetime will be stored in default format only while creating table and then you can change the display format in you select query the way you want using the Mysql Date Time Functions
This cannot be done for the table; besides, you even cannot change this default value at all.
The answer is a server variable datetime_format, it is unused.
Dim x as date
x = dr("appdate")
appdate = x.tostring("dd/MM/yyyy")
dr is the variable of datareader
try this:
DATE NOT NULL FORMAT 'YYYY-MM-DD'
I am trying to access values in Excel plugin via Officejs.
Office.context.document.getSelectedDataAsync(
Office.CoercionType.Matrix,
function(asyncResult => ...)
What I am looking for is an automatic way to access date values in their raw format without deduction if it is a date (initially, they are in Excel-like format, represented by a number of days after 01/01/1900).
Text of callback, but I can't find any interface to use so far.
Office.context.document.getSelectedDataAsync(zone, options, callback) returns numbers in all the cases.
You may try to get the value by using range.values API, then use setNumberFormat("dd/MM/yyyy") to set it into time format.
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)
I've just finished a video on Lynda.com and can't get the same results the instructor gets using D3. I've created a simpler version of the files in question, and it still doesn't work. The console outputs NaN when I try to retrieve a date.
The CSV, as output from Excel, is:
Date,AM
1995-04-16,3.5
1995-04-17,14.0
1995-04-18,10.8
and the Javascript file contains:
d3.csv("bg_test_date_parsing.csv", function(d) {
return { Date: +d.Date, AM: +d["AM"] };
}, function(data) {
console.log(data[1]);
});
The console in Mozilla gives me:
Object { Date: NaN, AM: 14 }
I can't proceed with any projects because I don't know where to start with importing dates properly. Videos on lynda.com just keep going, and I still don't get how to import and format dates. Also, the CSV originally was formatted 16-04-1995 as an example, outputted from a medical instrument, and I manually typed it into a year-month-day format that apparently D3 will work with. It took me many hours already using Google to get this far, so I'm obviously missing some key understanding.
+d.Date isn't working the way you want. Calling + on 16-04-1995 is resulting in NaN. You can't convert that string to a number like that.
You can convert the string like this, var myDate = new Date('04-16-1995')
Unfortunately, your format is day-month-year, and javascript wants month-day-year.
See this question for what to do about that:
Convert dd-mm-yyyy string to date
Update As #GerardoFurtado notes in his comment, the d3 library has some built in date parsing options that can convert dates in different formats. See the links in his comment.
Using d3, you could convert the string to a Date like this:
var myDate = d3.timeParse("%d-%m-%Y")('16-04-1995')
or, within your loop:
Date: d3.timeParse("%d-%m-%Y")(d.Date)
I'm trying to convert an HTML table to Excel in Javascript using new ActiveXObject("Excel.application"). Bascially I loop through table cells and insert the value to the corresponding cell in excel:
//for each table cell
oSheet.Cells(x,y).value = cell.innerText;
The problem is that when the cell is in date format of 'dd-mm-yyyy' (e.g. 10-09-2008), excel would read as 'mm-dd-yyyy' (i.e. 09 Oct 2008). I tried to specify NumberFormat like:
oSheet.Cells(x,y).NumberFormat = 'dd-mm-yyyy';
But, it has no effect. It seems that this only affect how excel display the value, not parse. My only solution now is to swap the date like:
var txt = cell.innerText;
if(/^(\d\d)-(\d\d)-\d\d\d\d$/.test(txt)) txt = txt.replace(/^(\d\d)-(\d\d)/,'$2-$1');
But, I'm worrying that it is not generic and a differnt machine setting would fail this.
Is there a way to specific how excel parse the input value?
You can avoid Excel's date parsing by entering the data using its native 'serial' date format. e.g '22nd Dec 08' is 39804 as an Excel serial date. (See here for a good explanation of these)
Then format the cell as you did before.
determine what culture-neutral date formats excel supports
use javascript to parse your date string and output in the an appropriate format
I don't know what formats excel supports but you'd want something like .net's round trip or sortable formats, where it will always be read consistently.
for #2, if you can trust javascript to construct an appropriate date from whatever string you feed it that's fine. if you're not sure about that you might look at a library like datejs where you can be more specific about what you want to happen.
Instead of
oSheet.Cells(x,y).NumberFormat = 'dd-mm-yyyy';
set this:
oSheet.Cells(x,y).NumberFormat = 14;
In Vbscript, we use to resolve this by
If IsDate ( Cell.Value ) Then
Cell.Value = DateValue ( Cell.Value )
End If
Maybe, In java script also you need to play with same approach.
I've tried your code but at end of the process, I re-applied format to the columns containing dates. It works fine, no matter what local language you have configurated yor machine.
Being my excel object defined as 'template', as soon as I got it data filled, I applied (just for example):
template.ActiveSheet.Range("D10:F99").NumberFormat = "dd/MMM/yyyy;#";
best regards