Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7628037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:32:31+00:00 2026-05-31T05:32:31+00:00

I use a proprietary date format that looks like this: var TheUserDate = 3.11.2012.4.3;

  • 0

I use a proprietary date format that looks like this:

var TheUserDate = "3.11.2012.4.3"; // is March 11th 2012 4:03AM

I created this format because my application uses a lot of timezone changes and I didn’t want to rely on the browser’s clock for that.

I have code like this 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 I rewrite this so that it emulated the .getMonth() .getYear() functions that are built into javascript. I’m thinking I need to modify the prototype of strings but this is my first attempt at doing something like this. I’d like to have functions like:

var TheMonth = TheUserDate.getMyMonth();
var TheDay = TheUserDate.getMyDay();
var TheYear = TheUserDate.getMyYear();
var TheDate = TheUserDate.getMyDate(); // to convert my format back to a javascript date.

How should I do this?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:32:31+00:00Added an answer on May 31, 2026 at 5:32 am

    Use the ISO8601 date format YYYY-MM-DD THH:mm:ssZ and there are datetime standardisation libraries for javascript, along with the UTC methods to avoid timezone issues. The biggest issue with Date values in JavaScript is that there is a lot of undefined behaviour in the Date objects. For anyone who needs a consistent Date api I would suggest using a fixed implementation that overrides the default Date object, allowing for consistent behaviour across all browsers, but do be careful if you have other libraries which are dependent on this.

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

    http://www.w3.org/TR/NOTE-datetime

    Does Javascript/EcmaScript3 support ISO8601 date parsing?

    Update

    As requested, to actually achieve what you want if you don’t want to use a standards compliant date format, this is how you might go about implementing the object you want

    var UserDate = (function () {
    
        function UserDate(dateString) {
            var dateArray= dateString.split('.'), i;
    
            if (dateArray.length !== 5) {
            // Handle this however you want, throw exception, 
            // bad date type etc
    
            }
    
            // No need to trim, parseInt doesn't care about 
            // leading or trailing whitespace
            for (i = 0; i < dateArray.length; i += 1) {
                dateArray[i] = parseInt(dateArray[i], 10);
            }
    
    
            // Check that the date array is well formed here if you want, 
            // check for NaN and value range
            // ...
    
            this._dateArray = dateArray;   
        }
    
        // Creates a date string from the internal date array
        UserDate.prototype.getDate = function () {
            var dateString = "", i;
    
            for (i = 0; i < this._dateArray.length; i += 1) {
                dateString += this._dateArray[i];
                if (i < this._dateArray.length - 1) {
                    dateString += ".";
                }
            }
    
            return dateString;
        };
    
        // Returns the day value from the internal date array
        UserDate.prototype.getDay = function () {
            return this._dateArray[0];
        };
    
        // Returns the month from the internal date array
        UserDate.prototype.getMonth = function () {
            return this._dateArray[1];
        };
    
        // Returns the year from the internal data array
        UserDate.prototype.getYear = function()  {
            return this._dateArray[2];
        };
    
        // Returns the hour from the internal date array
        UserDate.prototype.getHour = function()  {
            return this._dateArray[3];
        };
    
        // Returns the minute from the internal date array
        UserDate.prototype.getMinute = function()  {
            return this._dateArray[4];
        };
    
        // more prototypes here
    
    
        return UserDate;
    
    }());
    

    With my tests in the 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 to give you this functionality. It’s very bad practice to extend the native prototypes in JavaScript unless you are really sure what you are doing. It certainly doesn’t make any sense to be adding such bespoke functionality to a general purpose string object. If you need global access to this UserDate object then make it a global object as it would be in this example. The danger is that you don’t know what third party libraries are doing to the prototype.

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

    Might be worth a read if you’re interested in the arguments in general. If you really really really want to extend the string prototype then you could do something like

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

    But this will make me a sad chicken.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use some parts of Glassfish in a proprietary software that
Making an adobe flex ui in which data that is calculated must use proprietary
My company's proprietary software generates a log file that is much easier to use
So i have the misfortune of embedding this proprietary ActiveX control we created into
Okay so i have a packed a proprietary binary format. That is basically a
I am writing an application that requires me to take a proprietary bitmap format
I've got a proprietary program that I'm trying to use on a 64 bit
I'm using a proprietary library that has an openConnection function that I use as
Within our company we use a proprietary template engine, which stores its templates in
I’m trying to use the proprietary iPhone Safari properties -webkit-transition and -webkit-transform to make

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.