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

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…

Thanks!

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);

  
<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&#287;ustos 2007 &#199;ar&#351;amba 23:30:10 

*/

</script>

I want Firefox, Opera and Internet Explorer display 01 Ağustos 2007 Çarşamba 23:30:10
What will I do? Thanks…

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.

Whatever solution you use to get the last 2 digits, use getFullYear, since getYear is deprecated.

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


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;
	}
}



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()+‘’;


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’

it seems that it still out-puts the year in four digits

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