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: Convert getFullYear to 2-digit format - comp.lang.javascript ...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... Conversion: double IEEE 754 => decimal ASCII-String - comp.lang ...Convert getFullYear to 2-digit format - comp.lang.javascript ... Conversion: double IEEE 754 => decimal ASCII-String - comp.lang ... MMMMMMMM MMMMMMMM I need to convert ... 20 digit numeric value to be imported from an sql server - comp ...Convert getFullYear to 2-digit format - comp.lang.javascript ... 20 digit numeric value to be imported from an sql server - comp ... When i download it to SAS the last ... integer BCD converter in VHDL - comp.lang.vhdlConvert getFullYear to 2-digit format - comp.lang.javascript ... getFullYear returns a number, not a string. To ... back to signed ... 8 bit binary to 2 digit BCD - comp ... unsigned long -> ascii conversion - comp.lang.asm.x86Convert getFullYear to 2-digit format - comp.lang.javascript ..... to IEEE and I ... full 32-bit unsigned value into 10 decimal digits ... about writing a full binary to ... Convert string list to number list - comp.soft-sys.math ...... have an extracted list > of zero-padded two-digit ... recognize the numeric and not string format ... Am using an A2k app in which I want to convert a list of numbers (1,2,3 ... two's complement 8 bit data to decimal - comp.soft-sys.matlab ...Hi guys, does anyone knows how to convert a 8-bit data in the two's complement format back to signed ... 8 bit binary to 2 digit BCD - comp.lang.vhdl two's complement 8 ... awk script to convert dec to hex - comp.lang.awkIt should convert 128 , 51 , 34 and 40 each one into HEX... ... script n=0 hex=0 echo "Enter number in decimal format ... If HEX is single >digit (as true for dec vaules from 0 ... convert varchar to date - comp.databases.mysql20 digit numeric value to be imported from an sql server - comp ... If you're ... com/en-us/library/hh213505 ) can be very useful rather than using CONVERT to format date ... how to convert 1 to 000001? - comp.soft-sys.sashi guys, I need to convert number"1" into code"0000001 ... Each converted code needs to be 6 digits. is there ... but just display with excess zeros, just use FORMAT. Convert getFullYear to 2-digit format - comp.lang.javascript ...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... Convert getFullYear to 2-digit format - JavaScript / Ajax / DHTMLConvert getFullYear to 2-digit format. JavaScript / Ajax / DHTML Forums on Bytes. 7/23/2012 2:47:56 AM
|