SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: use of %?

  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    use of %?

    I'm trying to learn some JavaScript and haven't been able to get gain an understand of a usage of the % sign.

    I understand the use of % operator for modulo operations as in ...
    if (n % 10 == 1 && n % 100 != 11).

    But I haven't been able to clarify the usage of % in an expression like ...
    var sentence = today.getDateString('%day, %month %date%ordinal, %year');

    Any clarifications would be appreciated.

  2. #2
    Google Engineer polvero's Avatar
    Join Date
    Oct 2003
    Location
    Mountain View
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I personally only know the percentage symbol to be used for modulus operations in JavaScript. It does, however, do something a little bit different in Python I think. But I'm not sure. Most likely, if you're learning JavaScript, I wouldn't worry about it at all.

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    getDateString is not a standard function in Javascript, so what you have there is most likely a custom function where the % serves some purpose specific to the code you're using. Since the function appears to take only one parameter, I'm guessing it will then use split() to separate that string into useful variables.

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by btimms View Post
    I'm trying to learn some JavaScript and haven't been able to get gain an understand of a usage of the % sign.

    I understand the use of % operator for modulo operations as in ...
    if (n % 10 == 1 && n % 100 != 11).
    This use is the modulus operator which returns the remainder.

    Quote Originally Posted by btimms View Post
    var sentence = today.getDateString('%day, %month %date%ordinal, %year');
    This use i am not sure because i don't know the javascript function getDateString. So if this function is user defined and that does not mean for some standard use.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    Google Engineer polvero's Avatar
    Join Date
    Oct 2003
    Location
    Mountain View
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the example is also out of context, so as rajug pointed out, it may have been implemented by the author as such. For example there is no reference to where 'today' came from... it may have been an instance of some special date constructor
    Code:
    function SpecialDate() {
      // ...
    }
    SpecialDate.prototype.getDateString = function(d, m, y) {
      this.day = d.split('%')[1];
      this.month = m.split('%')[1];
      this.year = y.split('%')[1];
    }
    var today = new SpecialDate('%7', '%11', '%2007');
    As to why someone would do it like that? I don't know.

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by btimms View Post

    But I haven't been able to clarify the usage of % in an expression like ...
    var sentence = today.getDateString('%day, %month %date%ordinal, %year');
    Percent sign is traditionally used to specify "placeholders" for the formatting functions like printf. As already pointed out, "getDateString" is not the standard javascript, but I guess it's something similar to mysql date_format function.

    An example on how to write something like date_format in javascript

    Code:
    Date.prototype.format = function(fmt) {
    	var specs = {
    		// the list is very basic, but can be easily extended
    		d: this.getDate(),
    		m: this.getMonth() + 1,
    		y: this.getFullYear()
    	}
    	
    	return fmt.replace(/%([a-z])/gi, function($0, $1) { 
    		return specs[$1];
    	});
    }
    
    // example of use
    
    today = new Date;
    alert(today.format("%m/%d/%y"))

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the input. Where I saw this was from the Sitepoint book "JavaScript Anthology" in the date chapter. It is a custom function - here is the complete code:

    Date.prototype.getDateString = function(str)
    {
    var dnames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday',
    'Thursday', 'Friday', 'Saturday', 'Sunday'];

    var mnames = ['January', 'February', 'March', 'April',
    'May', 'June', 'July', 'August', 'September',
    'October', 'Novemeber', 'December'];

    str = str.replace('%day', dnames[this.getDay()]);
    str = str.replace('%date', this.getDate());
    str = str.replace('%ordinal', this.getDateOrdinal());
    str = str.replace('%month', mnames[this.getMonth()]);
    str = str.replace('%year', this.getFullYear());

    return str;
    };

    Date.prototype.getDateOrdinal = function()
    {
    var n = this.getDate();

    var ord = 'th';

    if (n % 10 == 1 && n % 100 != 11)
    {
    ord = 'st';
    }
    else if (n % 10 == 2 && n % 100 != 12)
    {
    ord = 'nd';
    }
    else if (n % 10 == 3 && n % 100 != 13)
    {
    ord = 'rd';
    }

    return ord;
    };

    Date.prototype.getISODate = function()
    {
    var mth = this.getMonth() + 1;
    mth = (mth < 10 ? '0' : '') + mth;

    var date = this.getDate();
    date = (date < 10 ? '0' : '') + date;

    return this.getFullYear() + mth + date;
    };

  8. #8
    Non-Member
    Join Date
    Aug 2007
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    &#37; Operator

    Description
    Divides two numbers and returns the remainder.

    Remarks
    The modulus, or remainder, operator divides number1 by number2 (rounding floating-point numbers to integers) and returns only the remainder as result. For example, in the following expression, A (which is result) equals 5.
    A = 19 % 6.7

  9. #9
    Google Engineer polvero's Avatar
    Join Date
    Oct 2003
    Location
    Mountain View
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep. As we all noted, this is for modulus operations, there is no doubt about that. Now that we can see the context it was used in, it makes sense now. They are indeed common place holder strings, and referencing the book example it all makes sense now.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •