0

I have two dates:

var first = '21-11-2012';
var second = '03-11-2012';

What is the best way to format it like this:

var first = '2012-11-21';
var second = '2012-11-03';

Should I use jQuery or simply JavaScript?

1

3 Answers 3

5

You don't need jQuery for this, simply use JavaScript like so:

function formatDate(d){
    return d.split('-').reverse().join('-');
}

Although if you want more reusable code consider using the JavaScript Date Object.

Sign up to request clarification or add additional context in comments.

2 Comments

Wouldn't hurt to simplify this by removing the redundant parts =.
so clean and perfect way :) +1
2

No need to be thinking of jQuery for basic string manipulation: the standard JS String methods are more than adequate, which (I assume) is why jQuery doesn't actually have equivalent methods.

A regex .replace() or the split/reverse/join concept in the other answers can both do it in one line. I'd recommend getting familiar with the methods at the MDN page I linked to, but meanwhile:

first = first.replace(/^(\d\d)-(\d\d)-(\d\d\d\d)$/,"$3-$2-$1");

(Same for second - encapsulate in a function if desired.)

This uses a regular expression to match the different parts of the date and reverse them.

UPDATE - As requested, an explanation of the regex I used:

^            // match beginning of string
(\d\d)       // match two digits, and capture them for use
             // in replacement expression as $1
-            // match the literal hyphen character
(\d\d)       // match two digits, and capture for use as $2
-            // match the literal hyphen character
(\d\d\d\d)   // match four digits, and capture for use as $3
$            // match end of string

Because the pattern matches from beginning to end of string the whole string will be replaced. The parts of the expression in parentheses are "captured" and can be referred to as $1, $2, etc. (numbered in the order they appear in the expression) if used in the replacement string, so "$3-$2-$1" reverses the order of these captured pieces and puts hyphens between them. (If the input string didn't meet that format the regex would not match and no replacement would be made.)

3 Comments

Can you please add few lines explaining your regex ? It would definitely help someone like me
( and ) capture for later use ?
@Jashwant - Yes. A full explanation is way beyond the scope of this question though, so I'd suggest you google up some regex tutorials (perhaps this one: regular-expressions.info/tutorial.html). There are some online regex testers if you want to play around with some examples. And although most regex syntax is common across different languages MDN lists the specific syntax applicable to JS.
1
first=first.split("-").reverse().join("-")
second=second.split("-").reverse().join("-")

Comments

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.