Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > date conv

Reply
Thread Tools

date conv

 
 
mike
Guest
Posts: n/a
 
      07-28-2005
I have a data like:

03-Jul-05

and need to convert it to:

7/3/2005

Is there a function for this already?

 
Reply With Quote
 
 
 
 
Matt Kruse
Guest
Posts: n/a
 
      07-28-2005
mike wrote:
> I have a data like:
> 03-Jul-05
> and need to convert it to:
> 7/3/2005
> Is there a function for this already?


There are a number of date function libs around.
Using mine from http://www.javascripttoolbox.com/date/ you could simply do:

var d = "03-Jul-05";
var formatted = formatDate(new
Date(getDateFromFormat(d,"d-MMM-yy")),"M/d/yyyy");

If your case is very specific, you could surely avoid using a big library
and instead use regular expressions to perform the task. Less code, but also
less flexibility. Depends on what you really need.

PS: Ignore John Stockton's comment which is surely coming, complaining about
converting from a good format to a dumb "American" one.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com


 
Reply With Quote
 
 
 
 
mike
Guest
Posts: n/a
 
      07-29-2005
yeah, i'm trying to understand what is going on in that script, not not
just plug a bunch of code in my page without learning anything.

thanks

 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      07-29-2005
JRS: In article <(E-Mail Removed)>, dated Thu, 28 Jul 2005
14:44:20, seen in news:comp.lang.javascript, Matt Kruse
<(E-Mail Removed)> posted :
>mike wrote:
>> I have a data like:
>> 03-Jul-05
>> and need to convert it to:
>> 7/3/2005
>> Is there a function for this already?

>
>There are a number of date function libs around.
>Using mine from http://www.javascripttoolbox.com/date/ you could simply do:
>
>var d = "03-Jul-05";
>var formatted = formatDate(new
>Date(getDateFromFormat(d,"d-MMM-yy")),"M/d/yyyy");
>
>If your case is very specific, you could surely avoid using a big library
>and instead use regular expressions to perform the task. Less code, but also
>less flexibility. Depends on what you really need.


Since he indicated an exact input format, it is reasonable to assume
that anything else is impossible or erroneous.


>PS: Ignore John Stockton's comment which is surely coming, complaining about
>converting from a good format to a dumb "American" one.


It's nice to see that at least one American here recognises that the US
format is dumb. There can be no doubt that M/D/YYYY is American; FFF is
used nowhere outside the unofficial American Empire (presumably much of
Iraq is now infested with it). So his quotes around 'American' are not
needed : OTOH, because he did not include 'dumb' within the quotes, it
is clear that he accepts the description 'dumb'.


To do the job only with a RegExp requires the use of a function in the
replacement. This looks simpler :

d = "03-Jul-05"
A = d.replace(/0?([1-9]?\d)-(\w\w\w)-(\d\d)/, "/$1/20$3")
A = "xxxJanFebMarAprMayJunJulAug...".indexOf(RegExp.$2 )/3 + A

Use of RegExp.$2 may be deprecated, but it will no doubt be possible to
avoid it.

Note that malformed input may give surprise results; if the input is not
assuredly [#]#-Mon-##, checks will be needed.

Note that the above could be easily adapted to provide Y M D as numbers,
which could easily be loaded into a Date Object, validated, and output
in a programmer-defined manner. See my pages js-date3.htm etc.

Eschew bloatware; it imposes an unnecessary burden on everyone who
downloads the code, which means all readers of pages using it. How many
lines do formatDate and getDateFromFormat need?

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      07-29-2005
JRS: In article <(E-Mail Removed) .com>
, dated Thu, 28 Jul 2005 17:25:34, seen in news:comp.lang.javascript,
mike <(E-Mail Removed)> posted :
>yeah, i'm trying to understand what is going on in that script, not not
>just plug a bunch of code in my page without learning anything.


Don't bother with bloatware, then.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
decimal to string conv Aj Python 2 02-27-2009 10:07 AM
Date Conv A.Dagostino ASP General 1 01-11-2006 11:28 AM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
from newbie: how specify # of times to invoke a regex; conv tuple to string Jeff Epler Python 2 04-21-2004 10:37 PM
2.4x Tele-conv. on A80? DHB Digital Photography 1 11-11-2003 08:57 PM



Advertisments