473,406 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Convert getFullYear to 2-digit format

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)
Jun 16 '06 #1
3 11821
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/
Jun 16 '06 #2
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
Jun 16 '06 #3
JRS: In article <kc******************************@comcast.com>, dated
Fri, 16 Jun 2006 15:45:02 remote, seen in news:comp.lang.javascript,
Randy Webb <Hi************@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.
Jun 18 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Lauren Quantrell | last post by:
I have a stored procedure using Convert where the exact same Convert string works in the SELECT portion of the procedure but fails in the WHERE portion. The entire SP is listed below....
1
by: Logan X via .NET 247 | last post by:
It's official....Convert blows. I ran a number of tests converting a double to an integer usingboth Convert & CType. I *ASSUMED* that CType would piggy-back ontop of Convert, and that performance...
4
by: Eric Lilja | last post by:
Hello, I've made a templated class Option (a child of the abstract base class OptionBase) that stores an option name (in the form someoption=) and the value belonging to that option. The value is...
6
by: David | last post by:
Hi, I have a script in one of my .asp pages which I think is written in VBScript (I did not write it). I would like to know how to do the following in Javascript. Have a combo on my page...
2
by: davidgordon | last post by:
Hi, I have some pages with this VBScript code, which obviously does not work in Firefox. How can I convert this to Javascript in order for my web page to work in Firefox ? It basically fills a...
7
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done...
4
by: Edwin Knoppert | last post by:
In my code i use the text from a textbox and convert it to a double value. I was using Convert.ToDouble() but i'm used to convert comma to dot. This way i can assure the text is correct. However...
1
by: johnlim20088 | last post by:
Hi, Currently I have 6 web projects located in Visual Source Safe 6.0, as usual, everytime I will open solution file located in my local computer, connected to source safe, then check out/check in...
6
by: Ken Fine | last post by:
This is a basic question. What is the difference between casting and using the Convert.ToXXX methods, from the standpoint of the compiler, in terms of performance, and in other ways? e.g. ...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.