javascript proprietary date format and prototype -


i use proprietary date format looks this:

var theuserdate = "3.11.2012.4.3"; // march 11th 2012 4:03am 

i created format because application uses lot of timezone changes , didn't want rely on browser's clock that.

i have code everywhere:

var datearray = theuserdate.split(".");  var themonth = parseint($.trim(datearray[0]), 10); var theday = parseint($.trim(datearray[1]), 10); var theyear = parseint($.trim(datearray[2]), 10); 

how can rewrite emulated .getmonth() .getyear() functions built javascript. i'm thinking need modify prototype of strings first attempt @ doing this. i'd have functions like:

var themonth = theuserdate.getmymonth(); var theday = theuserdate.getmyday(); var theyear = theuserdate.getmyyear(); var thedate = theuserdate.getmydate(); // convert format javascript date. 

how should this?

thanks.

use iso8601 date format yyyy-mm-dd thh:mm:ssz , there datetime standardisation libraries javascript, along utc methods avoid timezone issues. biggest issue date values in javascript there lot of undefined behaviour in date objects. needs consistent date api suggest using fixed implementation overrides default date object, allowing consistent behaviour across browsers, careful if have other libraries dependent on this.

i have used https://github.com/csnover/js-iso8601/ in past without issues

http://www.w3.org/tr/note-datetime

does javascript/ecmascript3 support iso8601 date parsing?

update

as requested, achieve want if don't want use standards compliant date format, how might go implementing object want

var userdate = (function () {      function userdate(datestring) {         var datearray= datestring.split('.'), i;          if (datearray.length !== 5) {         // handle want, throw exception,          // bad date type etc          }          // no need trim, parseint doesn't care          // leading or trailing whitespace         (i = 0; < datearray.length; += 1) {             datearray[i] = parseint(datearray[i], 10);         }           // check date array formed here if want,          // check nan , value range         // ...          this._datearray = datearray;        }      // creates date string internal date array     userdate.prototype.getdate = function () {         var datestring = "", i;          (i = 0; < this._datearray.length; += 1) {             datestring += this._datearray[i];             if (i < this._datearray.length - 1) {                 datestring += ".";             }         }          return datestring;     };      // returns day value internal date array     userdate.prototype.getday = function () {         return this._datearray[0];     };      // returns month internal date array     userdate.prototype.getmonth = function () {         return this._datearray[1];     };      // returns year internal data array     userdate.prototype.getyear = function()  {         return this._datearray[2];     };      // returns hour internal date array     userdate.prototype.gethour = function()  {         return this._datearray[3];     };      // returns minute internal date array     userdate.prototype.getminute = function()  {         return this._datearray[4];     };      // more prototypes here       return userdate;  }()); 

with tests in console:

> var somedate = new userdate("3.11.2012.4.3"); > somedate.getdate()       "3.11.2012.4.3" > somedate.getyear()   2012 > somedate.getmonth()   11 > somedate.getday()   3 > somedate.gethour()   4 > somedate.getminut()   3 

seriously don't consider editing string.prototype give functionality. it's bad practice extend native prototypes in javascript unless sure doing. doesn't make sense adding such bespoke functionality general purpose string object. if need global access userdate object make global object in example. danger don't know third party libraries doing prototype.

http://perfectionkills.com/extending-built-in-native-objects-evil-or-not/

might worth read if you're interested in arguments in general. if really want extend string prototype like

string.prototype.getday = function () {     return this.split('.')[0]; } 

but make me sad chicken.


Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -