Solved

javascript: month should be 2 chars (MM). How to prefix with "0" if 1 char is provided

Posted on 2014-11-04
12
121 Views
Last Modified: 2014-11-04
I have the following
       
var expireMonth = rq.SelectSingle("card/expire/month");
I want to check that if this month is one digit then prefix it with a "0" otherwise leave as 2 digit month
            

            
I tried the below but it doesn't prefix with "0"
        if (expireMonth .NormalizedText.length == 1)
            expireMonth = "0" + expireMonth ;

What am I doing wrong?
0
Comment
Question by:badtz7229
  • 5
  • 5
  • 2
12 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40422269
expireMonth  = ('0' + expireMonth).slice(-2) 

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 40422270
expireMonth = expireMonth .NormalizedText * 1 < 10 ? ( "0" + expireMonth ) : expireMonth;

Open in new window

0
 

Author Comment

by:badtz7229
ID: 40422323
@leakim9712:
I am getting "expireMonth.NormalizedText is undefined"
0
 
LVL 58

Expert Comment

by:Gary
ID: 40422352
Did you try my answer?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40422361
My answer depends from what you post, if it's undefined try without :
expireMonth = expireMonth * 1 < 10 ? ( "0" + expireMonth ) : expireMonth;

Open in new window

0
 

Author Comment

by:badtz7229
ID: 40422375
@Gary:
I get same error with ur solution
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 58

Expert Comment

by:Gary
ID: 40422376
How can you get the same error when NormalizedText is not in my code
0
 

Author Comment

by:badtz7229
ID: 40422398
oooh, i u see why
it is because after i'm checking if length ==1 and prefixing with "0"
i'm also checking

        if (expireMonth.NormalizedText.length != 2)
            throw "Month should be 2 characters";
0
 
LVL 58

Expert Comment

by:Gary
ID: 40422401
// If this returns a number
var expireMonth = rq.SelectSingle("card/expire/month");

// then you just need this and nothing else
expireMonth  = ('0' + expireMonth).slice(-2) 

Open in new window

0
 

Author Comment

by:badtz7229
ID: 40422456
@Gary:

what does .slice(-2) do ?
0
 
LVL 58

Expert Comment

by:Gary
ID: 40422478
The code adds a zero to the month then takes the last two digits so

1 becomes 01 and ends as 01

10 becomes 010 and ends as 10
0
 

Author Closing Comment

by:badtz7229
ID: 40422528
this worked. thanks.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now