0

I have this as my date: 1212009 so this should be like 12/1/2009 but I am stuck this as an id field and id fields cannot have dashes in there names.

Now can java-script take this number and re add the slashed in for me or how would I go about doing this?

Thanks

1
  • 1
    How do you know it's not 1/21/2009? Commented Dec 27, 2009 at 20:58

5 Answers 5

1

You should have two-digit numbers for the month and day, since "1212009" is ambiguous, could be interpreted as "1/21/2009" or "12/1/2009".

If you add the leading zeros, you know that there will be always 8 digits, so you can do something like this:

var str = "12012009"
var result = str.replace(/(\d{2})(\d{2})(\d{4})/, "$1/$2/$3");
// 12/01/2009

Or

var result = str.match(/(\d{2})(\d{2})(\d{4})/).slice(1).join('/');
// 12/01/2009
Sign up to request clarification or add additional context in comments.

6 Comments

Why do you use splice(1) instead of slice(1)? I don't see the point in also removing the first element in the match.
The input is supposed to be a number not a string though, so first you have to convert it into a string.
@Elijah: Yes you're right, splice is unnecessary since the Array reference will not be used anymore, I remove the first element, because it is the whole matched string.
s/first/elements after the first/
I did your first way and it seems to work. As far as the ambiguous part is I see your point. I just never noticed it since that is generated on my server end in C# and I used to shortdate(). I wonder why Microsoft choose to neglect the zero since it seems like it will cause problems if you ever need to convert it back to a date or something.
|
1

Well without leading zeros, it's not possible to make a reliable conversion. It's impossible to know if 1232009 is 1/23/2009 or 12/3/2009 without leading zeros. Also, your example number could be interpreted as 1/21/2009 too.

Comments

0

All you need to do is pass the string into the new date constructor.

Here is a good javascript resource for you to look at.

http://www.javascriptkit.com/jsref/date.shtml

1 Comment

To do this I would have to split the string apart to get year,month and day. I can't just through 12012009 into the date constructor it comes back as "not valid date".
0

Assuming that you add the leading zeroes to remove the ambiguity, you can format the date like so:

dateString = String(id).match(/(\d{2})(\d{2})(\d{4})/).slice(1).join('/')

Comments

-1

Maybe this will help you to get rid of the six or seven numbers long date (most important part is the first part):

var sDate = "1212009";
if(sDate.length == 7){
    sDate = sDate.substr(0,2) + "0" + sDate.substr(2);
}
else if(sDate.length == 6){
    sDate = "0" + sDate.substr(0,1) + "0" + sDate.substr(1);
}

// This part can be done with Regular Expressions, as shown in other comments
var month = parseInt(sDate.substring(0,2));
var day = parseInt(sDate.substring(2,4));
var year = parseInt(sDate.substring(4));

alert(day + "/" + month + "/" year);
// Will alert 12/1/2009

However, 1232009 will always be 01/23/2009, not 12/03/2009

112009 changes to 1/1/2009

10102009 changes to 10/10/2009

3 Comments

Why do you parse the strings to numbers just to put them back as strings? Second of all, you're missing the radix parameter of 10 for all of your parseInt calls.
If I parse it to string '01' will become '1', that's what chobo2 asks for, second, the radix parameter isn't necessary, since there will be no other values than 1-9
If you parse '08' or '09' without the radix argument, the result will be zero.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.