[ 10 posts ]

suchetagautham

  • Joined: Fri Jan 22, 2010 2:46 am
  • Posts: 33
  • Offline
  • Profile

Date formatting problem in IE 7,8

Post Posted: Wed Jun 23, 2010 3:41 am
+0-
Hello all,

I am using a custom formatter for displaying date . Both functions work fine in FF but not in IE 7/8 . Any help will be appreciated.

// Define a custom format function this is from YUI site as is
var myFormatDate = function (elCell, oRecord, oColumn, oData) {
var oDate = oData, sMonth;

switch(oDate.getMonth()) {
case 0:
sMonth = "Jan";
break;
case 1:
sMonth = "Feb";
break;
case 2:
sMonth = "Mar";
break;
case 3:
sMonth = "Apr";
break;
case 4:
sMonth = "May";
break;
case 5:
sMonth = "Jun";
break;
case 6:
sMonth = "Jul";
break;
case 7:
sMonth = "Aug";
break;
case 8:
sMonth = "Sep";
break;
case 9:
sMonth = "Oct";
break;
case 10:
sMonth = "Nov";
break;
case 11:
sMonth = "Dec";
break;
}
elCell.innerHTML = sMonth + " " + oDate.getDate() + ", " + oDate.getFullYear();
};

// This is using format under util.Date

var myFormatDate1 = function (elCell, oRecord, oColumn, oData) {
var oDate = new Date(oData);
elCell.innerHTML = YAHOO.util.Date.format(oDate, { format: "%b %d, %Y"}, 'en-US');;
};

// Column definitions
var myColumnDefs = [ // sortable:true enables sorting

{key:"Gender",label:"Gender"},
{key:"BirthDate",label:"DOB",formatter: myFormatDate1}

];


myDataSource.responseSchema = {
resultsList: "records",
fields: [

{key:"Gender"},
{key:"BirthDate",parser: 'date'}

]


" BirthDate" is of type date in MySql.
And setting parser to string displays date as in the db .


Thank you
Sucheta

Matt Parker

YUI Contributor

  • Username: mattatlamplight
  • Joined: Mon Apr 20, 2009 12:03 pm
  • Posts: 465
  • Location: London UK
  • GitHub: mattparker
  • Gists: mattparker
  • Offline
  • Profile

Re: Date formatting problem in IE 7,8

Post Posted: Wed Jun 23, 2010 5:04 am
+0-
Hi,

Two things: firstly, the date you send in your data needs to be something javascript can parse into a date object - ie something like mm/dd/yyyy - so you'll need to do some conversion on the server.

Secondly, take a look at this viewtopic.php?p=14172#p14172 - it might make your date formatter a bit smaller.

Matt

suchetagautham

  • Joined: Fri Jan 22, 2010 2:46 am
  • Posts: 33
  • Offline
  • Profile
Tags:

Re: Date formatting problem in IE 7,8

Post Posted: Wed Jun 23, 2010 9:27 pm
+0-
Thanks Matt.

This is what I did.

My BirthDate column in db held value as date and as say 2010-06-02 .

I made the following changes in my code :

fields: [

{key:"Gender"},
{key:"BirthDate",parser: "string"}

],


And changed my second format function myformatDate1 function as

var myFormatDate1 = function (elCell, oRecord, oColumn, oData) {
var year=oData.substr(0,4);
var month=oData.substr(5,2);
var day=oData.substr(8,2);
var oDate = new Date(year,month,day);
elCell.innerHTML = YAHOO.util.Date.format(oDate, { format: "%b %d, %Y"}, 'en-US');;
};


and assigned it as

var myColumnDefs = [ // sortable:true enables sorting

{key:"Gender",label:"Gender"},
{key:"BirthDate",label:"DOB",formatter: myFormatDate1}

];

It works in both browsers now.

Thanks
Sucheta

Satyam

YUI Contributor

  • Username: Satyam
  • Joined: Tue Dec 09, 2008 12:34 am
  • Posts: 2016
  • Location: Sitges, Spain
  • GitHub: Satyam
  • Gists: Satyam
  • IRC: DevaSatyam
  • YUI Developer
  • Offline
  • Profile

Re: Date formatting problem in IE 7,8

Post Posted: Thu Jun 24, 2010 5:48 am
+0-
Though it should work as you have it, it probably won't be fully functional. If you were to set that column as editable, the DateCellEditor would not work with a date stored as a string.

The right way to do it is to set a parser in the fields array of the responseSchema to parse the date in the format you receive it and return it as an instance of javaScript Date object. Then you can use the regular 'date' formatter with whatever dateOptions.format you want. You cannot use the built-in date parser for the date format you have. The built-in date parser uses JavaScript's own Date.parse method, which accepts only a few formats and not the YMD format you receive from your server. So, the solution is not on the side of the formatter as you have done but you should, instead, build your own parser for that YMD format.

suchetagautham

  • Joined: Fri Jan 22, 2010 2:46 am
  • Posts: 33
  • Offline
  • Profile

Re: Date formatting problem in IE 7,8

Post Posted: Thu Jun 24, 2010 9:11 pm
+0-
Thanks Satyam and thanks Matt too.


Going by your suggestion Satyam this is what I have done now and it works in both browsers.

1. Defined a custom parser

var myDateParser = function (oData) {
var year=oData.substr(0,4);
var month=oData.substr(5,2);
var day=oData.substr(8,2);
var oDate = new Date(year,month,day);
return oDate;
};

2. applied to my field

{key:"BirthDate",parser: myDateParser},

3.set Date formatter in column

{key:"BirthDate",label:"DOB",formatter:YAHOO.widget.DataTable.formatDate},


4.and used dateOptions in config as below


dateOptions: {format:"%b %d, %Y"}


Hope I have got it right now
BTW I am just displaying some data , no editing.

Thanks
Sucheta

Satyam

YUI Contributor

  • Username: Satyam
  • Joined: Tue Dec 09, 2008 12:34 am
  • Posts: 2016
  • Location: Sitges, Spain
  • GitHub: Satyam
  • Gists: Satyam
  • IRC: DevaSatyam
  • YUI Developer
  • Offline
  • Profile

Re: Date formatting problem in IE 7,8

Post Posted: Thu Jun 24, 2010 9:20 pm
+0-
You have it right now. Even if you don't edit that date right now, you might in some other page or other application in the future and it is best to have it right. A couple of tips, you could assign your parser to

YAHOO.util.DataSource.Parser.mySqlDate = function (oData) {
// your parser
}

Then you could use it as

{key:"BirthDate", parser:"mySqlDate"}

Also, in the col defs, you could say:

formatter:"date"

suchetagautham

  • Joined: Fri Jan 22, 2010 2:46 am
  • Posts: 33
  • Offline
  • Profile

Re: Date formatting problem in IE 7,8

Post Posted: Thu Jun 24, 2010 10:00 pm
+0-
Thank You so much.

I have done as suggested by you .

Sucheta

bhushan217

  • Joined: Fri Sep 17, 2010 7:08 am
  • Posts: 1
  • Offline
  • Profile
Tags:

Re: Date formatting problem in IE 7,8

Post Posted: Fri Sep 17, 2010 7:10 am
+0-
hey i hv done as said but it still doesnt work in IE :oops:

Satyam

YUI Contributor

  • Username: Satyam
  • Joined: Tue Dec 09, 2008 12:34 am
  • Posts: 2016
  • Location: Sitges, Spain
  • GitHub: Satyam
  • Gists: Satyam
  • IRC: DevaSatyam
  • YUI Developer
  • Offline
  • Profile

Re: Date formatting problem in IE 7,8

Post Posted: Fri Sep 17, 2010 8:47 am
+0-
So, it does in others? And even after doing it all still refuses to do it in IE?

suchetagautham

  • Joined: Fri Jan 22, 2010 2:46 am
  • Posts: 33
  • Offline
  • Profile

Re: Date formatting problem in IE 7,8

Post Posted: Fri Sep 17, 2010 11:24 pm
+0-
If you are seeing a mismatch in month [month in Javascript begins from 0 ].
you may have to add this

month=month-1;

in the custom method .


Sucheta
  [ 10 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum