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

S

santanu mishra

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
 
G

Gregor Kofler

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
 
S

Stevo

Gregor said:
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.
 
G

Gregor Kofler

Stevo meinte:
Gregor said:
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
 
E

Evertjan.

Gregor Kofler wrote on 22 apr 2008 in comp.lang.javascript:
Stevo meinte:
Gregor said:
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.
 
S

Stevo

Evertjan. said:
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.
 
E

Evertjan.

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

Stevo

Evertjan. said:
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.
 
G

Gregor Kofler

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
 
T

Tim Streater

[QUOTE="Stevo said:
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.[/QUOTE]

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

Dr J R Stockton

In comp.lang.javascript message <4da1f25e-7bd2-4a20-91ce-9b49d37394dd@v2
3g2000pro.googlegroups.com>, Tue, 22 Apr 2008 04:02:39, santanu mishra
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?
 
S

Stevo

Dr said:
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.
 
J

John G Harris

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
 
R

Rik Wasmus

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

Tim Streater

[QUOTE="Stevo said:
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.[/QUOTE]

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

Joost Diepenmaat

Dr J R Stockton said:
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" - ſ

Cheers,
J.
 
J

John G Harris

On Thu, 24 Apr 2008 12:27:22 +0200, John G Harris


'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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top