date conv

Discussion in 'Javascript' started by mike, Jul 28, 2005.

  1. mike

    mike Guest

    I have a data like:

    03-Jul-05

    and need to convert it to:

    7/3/2005

    Is there a function for this already?
     
    mike, Jul 28, 2005
    #1
    1. Advertising

  2. mike

    Matt Kruse Guest

    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
     
    Matt Kruse, Jul 28, 2005
    #2
    1. Advertising

  3. mike

    mike Guest

    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
     
    mike, Jul 29, 2005
    #3
  4. JRS: In article <>, dated Thu, 28 Jul 2005
    14:44:20, seen in news:comp.lang.javascript, Matt Kruse
    <> 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.
     
    Dr John Stockton, Jul 29, 2005
    #4
  5. JRS: In article <>
    , dated Thu, 28 Jul 2005 17:25:34, seen in news:comp.lang.javascript,
    mike <> 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)
     
    Dr John Stockton, Jul 29, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,361
    Michael Borgwardt
    May 30, 2004
  2. Jeff Epler
    Replies:
    2
    Views:
    290
    Avraham Makeler
    Apr 21, 2004
  3. Aj

    decimal to string conv

    Aj, Feb 27, 2009, in forum: Python
    Replies:
    2
    Views:
    472
  4. A.Dagostino

    Date Conv

    A.Dagostino, Jan 11, 2006, in forum: ASP General
    Replies:
    1
    Views:
    131
    Bob Barrows [MVP]
    Jan 11, 2006
Loading...

Share This Page