Solved

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

Posted on 2014-11-04
12
132 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dynamic Dropdowns 15 33
jquery tab header text 1 23
ASP.NET Content Page 3 35
Html value of radio 14 31
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
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…

820 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