SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Location
    HOTLANTA
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getYear(); I just want last two digits

    I'd like to only get the last two digits of a year when I use getYear(); it returns 107 and i just want 07 so i can display for example 09JAN07. Any tips on this?

    Thanks in advance

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To get the trailing 2 digits from any string you can use-
    str= str.match(/\d{2}$/);

    To get the last 2 digits from any year, make sure it is a string:

    var str=new Date().getFullYear()+'';
    str= str.match(/\d{2}$/);
    //returns '07' if the year is 2007..or 1907...
    Last edited by mrhoo; Jan 25, 2007 at 19:50.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Location
    HOTLANTA
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,840
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Since getFullYear() always returns 4 digits you could just use:

    var str=new Date().getFullYear().substr(3,2);
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Since getFullYear() always returns 4 digits you could just use:

    var str=new Date().getFullYear().substr(3,2);
    It returns them as a number.

    new Date().getFullYear().toString().substr(2, 2);
    Tab-indentation is a crime against humanity.

  6. #6
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
      
    <script type="text/javascript">
    
    var d = new Date("July 27, 2007 23:30:10") ;
    
    d.setDate(d.getDate() + 5);
    
    alert(d.toLocaleString() ) ; 
    
     /* Opera displays 01.08.2007 23:30:10  
      
    Firefox and Internet Explorer display 01 Ağustos 2007 &#199;arşamba 23:30:10 
    
    */
    
    </script>
    I want Firefox, Opera and Internet Explorer display 01 Ağustos 2007 &#199;arşamba 23:30:10
    What will I do? Thanks...
    Last edited by muazzez; Feb 4, 2007 at 14:36.

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,840
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Check if the configuration of your Opera browser allows you to change the locale information. If it doesn't then you have no way to get it to display the same as the other browsers. toLocaleString will return a free format text string based on both the browser being used and the locale settings on the computer and can't be guaranteed to be in any specific format.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whatever solution you use to get the last 2 digits, use getFullYear, since getYear is deprecated.

  9. #9
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Location
    HOTLANTA
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've got a problem happening here, with this code it keeps publishing the date as '07 even if the year it's parsing ends in '06

    Code:
    function createDateStr(date)
    {
    	if (date == null) { return "No or improper date provided"; }
    	
    	var str=new Date().getFullYear()+'';
    	str= str.match(/\d{2}$/);
    	
    	var date;
    	switch (date.getDate())
    	{
    		case 0: day = getLocalizedString("Sunday"); break;
    		case 1: day = getLocalizedString("Monday"); break;
    		case 2: day = getLocalizedString("Tuesday"); break;
    		case 3: day = getLocalizedString("Wednesday"); break;
    		case 4: day = getLocalizedString("Thursday"); break;
    		case 5: day = getLocalizedString("Friday"); break;
    		case 6: day = getLocalizedString("Saturday"); break;
    		default: day = "";
    	}
    
    	var month;
    	switch (date.getMonth())
    	{
    		case 0: month = getLocalizedString("JAN"); break;
    		case 1: month = getLocalizedString("FEB"); break;
    		case 2: month = getLocalizedString("MAR"); break;
    		case 3: month = getLocalizedString("APR"); break;
    		case 4: month = getLocalizedString("MAY"); break;
    		case 5: month = getLocalizedString("JUN"); break;
    		case 6: month = getLocalizedString("JUL"); break;
    		case 7: month = getLocalizedString("AUG"); break;
    		case 8: month = getLocalizedString("SEPT"); break;
    		case 9: month = getLocalizedString("OCT"); break;
    		case 10: month = getLocalizedString("NOV"); break;
    		case 11: month = getLocalizedString("DEC"); break;
    		default: month = "";
    	}
    		if (day == "" && month == "")
    	{
    		return null;
    	}
    	else {
    		return date.getDate() + " " + month + " " + str;
    	}
    }

  10. #10
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var str=new Date().getFullYear() is the current date's full year.

    You threw away the date argument.


    If datestring is a string that will parse to a Date object:
    var yearstring=new Date(datestring).getFullYear()+'';

    or if dateobj is a Date object- var yearstring=dateobj.getFullYear()+'';


    Code:
    function createDateStr(D){
    	if(D==undefined) return 'No date';
    	if(D===true)D=new Date();
    	try{
    		D= new Date(D);
    		var str= D.getFullYear()+'';
    		if(isNaN(str))throw new Error();
    		else str= str.slice(-2);
    	}
    	catch(er){
    		return 'Bad Date';
    	}
    	var daynames=['Sun','Mon','Tue','Wed','Thurs','Fri','Sat'];
    	var monthnames=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
    	str=daynames[D.getDay()]+' '+monthnames[D.getMonth()]+ ' '+str;
    	return str;
    }
    createDateStr(true);
    //testing:

    createDateStr(true)// returns current date: Thurs Feb 07
    createDateStr(); // returns 'No date':
    createDateStr('2001,4,23'); // returns: 'Mon Apr 01'
    createDateStr(new Date(2001,00,23))// returns Tue Jan 01;
    createDateStr('tomato')// returns 'Bad Date'
    Last edited by mrhoo; Feb 22, 2007 at 20:46. Reason: correction

  11. #11
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Location
    HOTLANTA
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it seems that it still out-puts the year in four digits

  12. #12
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My mistake- try block should be:
    try{
    D= new Date(D);
    var str= D.getFullYear();
    if(isNaN(str))throw new Error();
    else str= (str+'').slice(-2);
    }
    without the parentheses slice is slicing the empty string, not the year string


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
  •