By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,146 Members | 1,847 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,146 IT Pros & Developers. It's quick & easy.

Need of a code snipet which converts mm/dd/yy to dd/mm/yy

P: n/a
Hi ,
I am stuck with a requirement from my client to change the date
format from mm/dd/yy to dd/mm/yy .If any body can help me out with
this regard as its very much urgent.


Regards,
Santanu
Jun 27 '08 #1
Share this Question
Share on Google+
19 Replies


P: n/a
On 22 Apr, 11:02, santanu mishra <mishra.sant...@gmail.comwrote:
Hi ,
I am stuck with a requirement from my client to change the date
format from mm/dd/yy to dd/mm/yy .If any body can help me out with
this regard as its very much urgent.

Regards,
Santanu
I use:
http://www.koders.com/javascript/fid...4E48720E2.aspx
Jun 27 '08 #2

P: n/a
santanu mishra meinte:
Hi ,
I am stuck with a requirement from my client to change the date
format from mm/dd/yy to dd/mm/yy .If any body can help me out with
this regard as its very much urgent.
Untested:

function convert(dateString) {
var new = dateString.split("/");
return new[1]+"/"+new[0]+"/"+new[2];
}

Gregor
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Jun 27 '08 #3

P: n/a
Gregor Kofler wrote:
santanu mishra meinte:
> I am stuck with a requirement from my client to change the date
format from mm/dd/yy to dd/mm/yy .If any body can help me out with
this regard as its very much urgent.

Untested:
function convert(dateString) {
var new = dateString.split("/");
return new[1]+"/"+new[0]+"/"+new[2];
}
If that doesn't work, try replacing the variable name new with a
different name. I wouldn't be surprised if that's a problem.
Jun 27 '08 #4

P: n/a
Stevo meinte:
Gregor Kofler wrote:
>santanu mishra meinte:
>> I am stuck with a requirement from my client to change the date
format from mm/dd/yy to dd/mm/yy .If any body can help me out with
this regard as its very much urgent.

Untested:
function convert(dateString) {
var new = dateString.split("/");
return new[1]+"/"+new[0]+"/"+new[2];
}

If that doesn't work, try replacing the variable name new with a
different name. I wouldn't be surprised if that's a problem.
Yes, my mistake. Something like "newDateString" is definitely better,
than a reserved word.

Gregor

--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Jun 27 '08 #5

P: n/a
Gregor Kofler wrote on 22 apr 2008 in comp.lang.javascript:
Stevo meinte:
>Gregor Kofler wrote:
>>santanu mishra meinte:
I am stuck with a requirement from my client to change the date
format from mm/dd/yy to dd/mm/yy .If any body can help me out with
this regard as its very much urgent.

Untested:
function convert(dateString) {
var new = dateString.split("/");
return new[1]+"/"+new[0]+"/"+new[2];
}

If that doesn't work, try replacing the variable name new with a
different name. I wouldn't be surprised if that's a problem.

Yes, my mistake. Something like "newDateString" is definitely better,
than a reserved word.
function convert(d) {
return (d = d.split('/'))[1]+'/'+d[0]+'/'+d[2];
};

or

function convert(d) {
return d.replace(/(\d\d\/)(\d\d\/)/,'$2$1');
};

both tested IE7.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 27 '08 #6

P: n/a
Evertjan. wrote:
Gregor Kofler wrote on 22 apr 2008 in comp.lang.javascript:
>Stevo meinte:
>>Gregor Kofler wrote:
santanu mishra meinte:
I am stuck with a requirement from my client to change the date
format from mm/dd/yy to dd/mm/yy .If any body can help me out with
this regard as its very much urgent.
Untested:
function convert(dateString) {
var new = dateString.split("/");
return new[1]+"/"+new[0]+"/"+new[2];
}
If that doesn't work, try replacing the variable name new with a
different name. I wouldn't be surprised if that's a problem.
Yes, my mistake. Something like "newDateString" is definitely better,
than a reserved word.

function convert(d) {
return (d = d.split('/'))[1]+'/'+d[0]+'/'+d[2];
};

or

function convert(d) {
return d.replace(/(\d\d\/)(\d\d\/)/,'$2$1');
};

both tested IE7.
I'd stick with what Gregor created. It's simple, efficient and easy to
understand by whoever gets to maintain the code in the future.
Jun 27 '08 #7

P: n/a
Stevo wrote on 22 apr 2008 in comp.lang.javascript:
>function convert(d) {
return (d = d.split('/'))[1]+'/'+d[0]+'/'+d[2];
};

or

function convert(d) {
return d.replace(/(\d\d\/)(\d\d\/)/,'$2$1');
};

both tested IE7.

I'd stick with what Gregor created. It's simple, efficient and easy to
understand by whoever gets to maintain the code in the future.
As you wish, Stevo. Why is it efficient?

The above is simple javascript.
I wouldn't recomment having my code maintained
by someone that does not understand the above.

And like all good functions, it can be seen as a black box from outside.

You could feel the need to add all kinds of validation for bad input,
however.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 27 '08 #8

P: n/a
Evertjan. wrote:
Stevo wrote on 22 apr 2008 in comp.lang.javascript:
>>function convert(d) {
return (d = d.split('/'))[1]+'/'+d[0]+'/'+d[2];
};
or
function convert(d) {
return d.replace(/(\d\d\/)(\d\d\/)/,'$2$1');
};
both tested IE7.
I'd stick with what Gregor created. It's simple, efficient and easy to
understand by whoever gets to maintain the code in the future.
As you wish, Stevo. Why is it efficient?
I just mean that it's simple, efficient ENOUGH, and immediately obvious
(without more than 2 seconds of thought) what it's doing. If you know
what the split method does, then you know what the function is doing.
The above is simple javascript.
I wouldn't recomment having my code maintained
by someone that does not understand the above.
We see almost every day people coming here that are given the task by
their employers of changing some code that they don't understand. We
might think that's a dumb idea, but it's happening all the time.

I'm not a fan of regexp constructs because of their lack of readability
to those unfamiliar with them, and that is a large proportion of
programmers that just never get into them. I don't mind admitting that I
fall into that group too. So the only reason I know what your second
function is doing, is because I know what job it has to do from the OP's
original requirements. If I didn't have that knowledge, then I'd be
wondering what this $2$1 business is all about. I've deduced that what
you've got in parentheses is looking for digit, digit, forward-slash and
when it finds one it generates the $1 token, then it looks again for
digit, digit forward slash and generates the $2 token, then the replace
happens and $2 and $1 get swapped in the returned string. If I didn't
have those OP requirements, I can guarantee I'd be at wikipedia's
regular expression page trying to find out what's going on. Don't get me
wrong, I'm impressed with it, but I'd never use it.
Jun 27 '08 #9

P: n/a
Evertjan. meinte:
function convert(d) {
return (d = d.split('/'))[1]+'/'+d[0]+'/'+d[2];
};

or

function convert(d) {
return d.replace(/(\d\d\/)(\d\d\/)/,'$2$1');
};

both tested IE7.
Agreed. But optimization should be the last step. Besides, I suppose the
OP might have problems with understanding the condensed code or the reg
ex (which might be slower than the split).

Gregor
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Jun 27 '08 #10

P: n/a
In article <fu*************@news.t-online.com>, Stevo <no@mail.invalid>
wrote:
Evertjan. wrote:
Stevo wrote on 22 apr 2008 in comp.lang.javascript:
>function convert(d) {
return (d = d.split('/'))[1]+'/'+d[0]+'/'+d[2];
};
or
function convert(d) {
return d.replace(/(\d\d\/)(\d\d\/)/,'$2$1');
};
both tested IE7.
I'd stick with what Gregor created. It's simple, efficient and easy to
understand by whoever gets to maintain the code in the future.
As you wish, Stevo. Why is it efficient?

I just mean that it's simple, efficient ENOUGH, and immediately obvious
(without more than 2 seconds of thought) what it's doing. If you know
what the split method does, then you know what the function is doing.
The above is simple javascript.
I wouldn't recomment having my code maintained
by someone that does not understand the above.

We see almost every day people coming here that are given the task by
their employers of changing some code that they don't understand. We
might think that's a dumb idea, but it's happening all the time.

I'm not a fan of regexp constructs because of their lack of readability
to those unfamiliar with them, and that is a large proportion of
programmers that just never get into them. I don't mind admitting that I
fall into that group too. So the only reason I know what your second
function is doing, is because I know what job it has to do from the OP's
original requirements. If I didn't have that knowledge, then I'd be
wondering what this $2$1 business is all about. I've deduced that what
you've got in parentheses is looking for digit, digit, forward-slash and
when it finds one it generates the $1 token, then it looks again for
digit, digit forward slash and generates the $2 token, then the replace
happens and $2 and $1 get swapped in the returned string. If I didn't
have those OP requirements, I can guarantee I'd be at wikipedia's
regular expression page trying to find out what's going on. Don't get me
wrong, I'm impressed with it, but I'd never use it.
I agree 100% with this. I almost never use regexp for just this reason,
because they are unreadable. This doesn't stop me writing large apps
which are efficient enough.
Jun 27 '08 #11

P: n/a
In comp.lang.javascript message <4da1f25e-7bd2-4a20-91ce-9b49d37394dd@v2
3g2000pro.googlegroups.com>, Tue, 22 Apr 2008 04:02:39, santanu mishra
<mi************@gmail.composted:
>Hi ,
I am stuck with a requirement from my client to change the date
format from mm/dd/yy to dd/mm/yy .If any body can help me out with
this regard as its very much urgent.
St = "mm/dd/yy"
St1 = St.substring(3,6) + St.substring(0,3) + St.substring(6)
St2 = St.substr(3,3) + St.substr(0,3) + St.substr(6)
St3 = St.replace(/(..).(..)/, "$2/$1")
x = [St1, St2, St3].join(" ") // to show results

Note that it will also convert "dd/mm/yy" to "mm/dd/yy"; thus it can be
sold in the USA.

Those who decline to learn simple RegExp use are wasting their own
future time, unless they decide to give up programming in favour of a
less intellectual pursuit, such as politics.

An OP who needs to ask such a question should do likewise.

Aside : is there a representation, known to browsers, for the old-style
s as in "Ye olde faufage-fhop" in which I used f instead?

--
(c) John Stockton, nr London UK. replyYYWW merlyn demon co uk Turnpike 6.05.
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html-Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm: about usage of News.
No Encoding. Quotes precede replies. Snip well. Write clearly. Mail no News.
Jun 27 '08 #12

P: n/a
On Wed, 23 Apr 2008 19:27:55 +0200, Dr J R Stockton
<jr*@merlyn.demon.co.ukwrote:
Aside : is there a representation, known to browsers, for the old-style
s as in "Ye olde faufage-fhop" in which I used f instead?
&#x17F; or ſ
--
Rik Wasmus
Jun 27 '08 #13

P: n/a
Dr J R Stockton wrote:
Those who decline to learn simple RegExp use are wasting their own
future time, unless they decide to give up programming in favour of a
less intellectual pursuit, such as politics.

An OP who needs to ask such a question should do likewise.
Is there any need for that?

There are a lot of us that don't consider RegExp a good alternative.

Imagine how much code is being run in the background to parse a RegExp
string. In this particular example, the code that Gregor provided, which
was a simple array split, followed by a simple concatenation, was (a)
extremely easy to understand, and (2) doesn't involve very complex
regexp parsing in the background. The code that handles the split and
concatenation code behind the scenes we can all imagine is fairly
trivial. The same can't be said for a RegExp parser.

I prefer two lines of code that are easy to understand and as far as
complexity goes, what you see is pretty much all that's happening. The
RegExp version is not obvious if you're unfamiliar, and involves too big
an overhead in the background. Certainly too big for this particular
case. If you're parsing a humongous length string then the RegExp
overhead would pay off because the code you'd have to write wouldn't be
just two lines.

I have used RegExp code before, I always fully comment what it does
though, and if the alternative (as in this case) is a better solution,
then I'll always prefer that.
Jun 27 '08 #14

P: n/a
On Wed, 23 Apr 2008 at 18:27:55, in comp.lang.javascript, Dr J R
Stockton wrote:

<snip>
>Aside : is there a representation, known to browsers, for the old-style
s as in "Ye olde faufage-fhop" in which I used f instead?
The HTML character entities don't include a long s, but they do include
the integral sign, &int; , which might do instead. It works in IE5.5+.
Failing that, you'll have to use a small image instead.

Incidentally, some early English printing displayed 'the' as a y with a
dot over it. Presumably a hand-written 'ye' was often abbreviated that
way.

John
--
John Harris
Jun 27 '08 #15

P: n/a
In comp.lang.javascript message <op***************@metallium.lan>, Wed,
23 Apr 2008 22:12:34, Rik Wasmus <lu************@hotmail.composted:
>On Wed, 23 Apr 2008 19:27:55 +0200, Dr J R Stockton
<jr*@merlyn.demon.co.ukwrote:
>Aside : is there a representation, known to browsers, for the old-style
s as in "Ye olde faufage-fhop" in which I used f instead?

&#x17F; or ſ
Thanks. I'm copying from a Book of 1662. Tested OK in IE7, FF2, Op9,
Sf2. In <URL:http://www.merlyn.demon.co.uk/estr-bcp.htm#PTs>.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
Web <URL:http://www.merlyn.demon.co.uk/- w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/- see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jun 27 '08 #16

P: n/a
On Thu, 24 Apr 2008 12:27:22 +0200, John G Harris
<jo**@nospam.demon.co.ukwrote:
On Wed, 23 Apr 2008 at 18:27:55, in comp.lang.javascript, Dr J R
Stockton wrote:

<snip>
>Aside : is there a representation, known to browsers, for the old-style
s as in "Ye olde faufage-fhop" in which I used f instead?

The HTML character entities don't include a long s, but they do include
the integral sign, &int; , which might do instead. It works in IE5.5+.
Failing that, you'll have to use a small image instead.

Incidentally, some early English printing displayed 'the' as a y with a
dot over it. Presumably a hand-written 'ye' was often abbreviated that
way.
'y' was a substitution for the thorn symbol (þ), for which there was
no letter in print AFAIK. It would still be pronounced as 'th', making a
fool out of those saying a literal 'ye' nowadays. As far as I know, no
diacretics were added to this, but they were to the thorn character, which
can look like a y in written form, see
<http://en.wikipedia.org/wiki/Ž#Abbreviations>.
--
Rik Wasmus
Jun 27 '08 #17

P: n/a
In article <fu*************@news.t-online.com>, Stevo <no@mail.invalid>
wrote:
Dr J R Stockton wrote:
Those who decline to learn simple RegExp use are wasting their own
future time, unless they decide to give up programming in favour of a
less intellectual pursuit, such as politics.

An OP who needs to ask such a question should do likewise.

Is there any need for that?

There are a lot of us that don't consider RegExp a good alternative.

Imagine how much code is being run in the background to parse a RegExp
string. In this particular example, the code that Gregor provided, which
was a simple array split, followed by a simple concatenation, was (a)
extremely easy to understand, and (2) doesn't involve very complex
regexp parsing in the background. The code that handles the split and
concatenation code behind the scenes we can all imagine is fairly
trivial. The same can't be said for a RegExp parser.

I prefer two lines of code that are easy to understand and as far as
complexity goes, what you see is pretty much all that's happening. The
RegExp version is not obvious if you're unfamiliar, and involves too big
an overhead in the background. Certainly too big for this particular
case. If you're parsing a humongous length string then the RegExp
overhead would pay off because the code you'd have to write wouldn't be
just two lines.
Then the regexp would be *really* incomprehensible :-)
I have used RegExp code before, I always fully comment what it does
though, and if the alternative (as in this case) is a better solution,
then I'll always prefer that.
Dead right. Let's have some respect for future maintainers.
Jun 27 '08 #18

P: n/a
Dr J R Stockton <jr*@merlyn.demon.co.ukwrites:
Aside : is there a representation, known to browsers, for the old-style
s as in "Ye olde faufage-fhop" in which I used f instead?
You're looking for the unicode "LATIN SMALL LETTER LONG S" - &#x017f;

Cheers,
J.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jun 27 '08 #19

P: n/a
On Thu, 24 Apr 2008 at 20:02:17, in comp.lang.javascript, Rik Wasmus
wrote:
>On Thu, 24 Apr 2008 12:27:22 +0200, John G Harris
<jo**@nospam.demon.co.ukwrote:
<snip>
>Incidentally, some early English printing displayed 'the' as a y with
a dot over it. Presumably a hand-written 'ye' was often abbreviated
that way.

'y' was a substitution for the thorn symbol (þ), for which there
was no letter in print AFAIK.
I'm following a facsimile of one of the first books to be printed in
England. There were no letters in print at all until the printer carved
them himself or had them carved to his design. Presumably the printer
made thorn look like a y because that's what his customers were familiar
with in hand-written books.

>It would still be pronounced as 'th', making a fool out of those
saying a literal 'ye' nowadays. As far as I know, no diacretics were
added to this, but they were to the thorn character, which can look
like a y in written form, see <http://en.wikipedia.org/wiki/Ž#Abbrevia
tions>.
The dot over y is not an accent. It's an abbreviation, just as 'at' is
abbreviated as @ and first is abbreviated as 1st.

John
--
John Harris
Jun 27 '08 #20

This discussion thread is closed

Replies have been disabled for this discussion.