Convert getFullYear to 2-digit format

  • Follow


I'm trying to convert the current year into a 2-digit format.  Since 
getYear returns 106, I'm trying to perform a substring of getFullYear() 
with no luck.  What am doing wrong?

currentTime = new Date()

var result = "FY" + currentTime.getFullYear().substring(2, 4)
0
Reply funkjunk (48) 6/16/2006 7:22:17 PM

O.B. said the following on 6/16/2006 3:22 PM:
> I'm trying to convert the current year into a 2-digit format.  Since 
> getYear returns 106, I'm trying to perform a substring of getFullYear() 
> with no luck.  What am doing wrong?
> 
> currentTime = new Date()
> 
> var result = "FY" + currentTime.getFullYear().substring(2, 4)

var result = "FY" + (currentTime.getFullYear())%100;

Or, just use getYear and do the same thing with %100

-- 
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
0
Reply Randy 6/16/2006 7:45:02 PM


Randy Webb wrote:
> O.B. said the following on 6/16/2006 3:22 PM:
>> I'm trying to convert the current year into a 2-digit format.  Since 
>> getYear returns 106, I'm trying to perform a substring of 
>> getFullYear() with no luck.  What am doing wrong?

getFullYear returns a number, not a string.  To use the String object's 
substring method, convert the result to a string by concatenating an 
empty string to the result of getFullYear:

    stringYear = currentTime.getFullYear() + '';


>> currentTime = new Date()
>>
>> var result = "FY" + currentTime.getFullYear().substring(2, 4)

If only ever dealing with 4 digit years, then:

   var result = FY + (currentTime.getFullYear() + '').substring(2,4);


You could also use the replace method:

   var result = ('FY' + currentTime.getFullYear()).replace(/\d\d/,'');


But using modulus operator is probably neater...

> var result = "FY" + (currentTime.getFullYear())%100;
> 
> Or, just use getYear and do the same thing with %100

For  years yy00 to yy09 that will return a single digit, the OP may want 
to add a leading zero:

   result = (result < 10)? '0' + result : '' + result;


-- 
Rob
0
Reply RobG 6/16/2006 11:22:04 PM

JRS:  In article <kcOdncy1ZZGzlw7ZnZ2dnUVZ_uWdnZ2d@comcast.com>, dated
Fri, 16 Jun 2006 15:45:02 remote, seen in news:comp.lang.javascript,
Randy Webb <HikksNotAtHome@aol.com> posted :
>O.B. said the following on 6/16/2006 3:22 PM:
>> I'm trying to convert the current year into a 2-digit format.  Since 
>> getYear returns 106, I'm trying to perform a substring of getFullYear() 

getYear does not necessarily return 106; it can return 2006 or 6,
depending on browser.

On the Internet,   getYear   should only be used if either the year will
be in 1900..1999 or it is effectively followed by code restricting the
value to 0..99 or "00".."99".

>> with no luck.  What am doing wrong?

Don't rely on luck; read the newsgroup FAQ, carefully.


>> currentTime = new Date()
>> 
>> var result = "FY" + currentTime.getFullYear().substring(2, 4)
>
>var result = "FY" + (currentTime.getFullYear())%100;
>
>Or, just use getYear and do the same thing with %100

The OP, in Subject and Body, calls for a 2-digit format.  Your code, for
10% of years including around now, gives 1-digit.

R = "FY" + String(currentTime.getFullYear()).substring(2, 4)

R = ("FY" + currentTime.getFullYear()).replace(/(..)..(..)/, "$1$2")

R = "FY" + LZ(currentTime.getYear()%100)
 
-- 
 � John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v4.00   IE 4 �
 <URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript 
 <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
 <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
0
Reply Dr 6/18/2006 1:36:28 PM

3 Replies
375 Views

(page loaded in 0.048 seconds)

Similiar Articles:













7/23/2012 2:47:56 AM


Reply: