JavaScript - IE,Chrome shows different years Why?

Asked By abdul rehman on 24-Sep-13 09:22 AM

var myDate = new Date("1/1/08");

var dayOfMonth = myDate.getDate();

myDate.setDate(dayOfMonth);

alert(myDate);

 var date = new Date("10-01-08");

            var dayOfMonth = date.getDate();

       var y= date.setDate(dayOfMonth);

alert(date.getFullYear());

test in js fiddle in IE and Chrome or firefox

http://jsfiddle.net/

plese help!

Erik Little replied to abdul rehman on 28-Feb-14 02:24 PM
If at all possible I would not try to format dates in JavaScript.

You're best bet is to format your dates and send it to the client as a string and if a time needs to be saved on the server work with the server side DateTime object and save your date and or date and time from the time created on the server when the request first hits the web server.

In the case where your working with APIs' from places like Facebook, LinkedIn, Twitter, ECT... you have to get familiar with each of their ways of send dates and time info to you and write your JavaScript to accommodate each API.

Here is a few little pieces of code that can help you out, and if you do not have one yet create your own uiUitl to store UI helper related JavaScript code.

var parseTimeStamp = function (strDate, timeType) {
     /// <param name="timeType" type="String">Central,Mountain,Eastern,Pacific,Alaskan</param>
     timeType = timeType || 'central';
     switch (timeType.toLowerCase()) {
       //
       case 'central':
         return new Date(strDate).toLocaleString().replace('GMT-0600 (Central Standard Time)', '');
         break;
     };
     return '';
   };
   var getMonthName = function () {
 
     var month = new Array();
     month[0] = "January";
     month[1] = "February";
     month[2] = "March";
     month[3] = "April";
     month[4] = "May";
     month[5] = "June";
     month[6] = "July";
     month[7] = "August";
     month[8] = "September";
     month[9] = "October";
     month[10] = "November";
     month[11] = "December";
 
     return month[new Date().getMonth()];
   }
   var getDayName = function () {
 
     var day = new Array(7);
     day[0] = "Sunday";
     day[1] = "Monday";
     day[2] = "Tuesday";
     day[3] = "Wednesday";
     day[4] = "Thursday";
     day[5] = "Friday";
     day[6] = "Saturday";
 
     return day[new Date().getDay()];
   };