converting Date() to unix timestamp format?

Discussion in 'Javascript' started by marc@gaspdesign.co.uk, Mar 6, 2007.

  1. Guest

    hi

    im trying to convert Date() into a unix timestamp so i can stick the
    result into a mysql db, please help!
     
    , Mar 6, 2007
    #1
    1. Advertising

  2. Lee Guest

    said:
    >
    >hi
    >
    >im trying to convert Date() into a unix timestamp so i can stick the
    >result into a mysql db, please help!


    I've been using Unix since before you were born (I can tell by
    the fact that you haven't learned to use the SHIFT key), and
    have no idea what you mean by a "unix timestamp" and don't have
    any inclination to look at mysql documentation to see what they
    think it is.

    Give an example of what you want.


    --
     
    Lee, Mar 6, 2007
    #2
    1. Advertising

  3. Lee wrote:

    > said:
    >> im trying to convert Date() into a unix timestamp so i can
    >> stick the result into a mysql db, please help!

    >
    > I've been using Unix since before you were born (I can tell by
    > the fact that you haven't learned to use the SHIFT key), and
    > have no idea what you mean by a "unix timestamp"


    Then you should definitely start learning about it!

    http://en.wikipedia.org/wiki/Unix_Timestamp
    http://www.unixtimestamp.com

    > and don't have any inclination to look at mysql documentation
    > to see what they think it is.


    The Unix timestamp is well-known and defined; it is the number of
    seconds since Epoch. But the MySQL timestamp is different, that uses
    the YYYYMMDDHHmmSS-format. Since the objective is to store a timestamp
    in MySQL, one should opt to play by the MySQL rules here. Suppose this
    table:

    CREATE TABLE `tt` (`a` timestamp(14) NOT NULL);

    If the OP's intention is to insert the current time in db -which I
    suspect- then just do:

    INSERT INTO `tt` VALUES ( NOW() );

    And then one could read it out as Unix timestamp:

    SELECT UNIX_TIMESTAMP(a) FROM tt;

    No need for any javascript or shell commands.

    If the intention is to store a timestamp that is not the current one,
    then let javascript's Date() calculate the specific time of your
    interest in (MySQL's) YYYYMMDDHHmmSS-format, like eg. 20040318140523
    for March 18 2004, 14:05:23. Then do:

    INSERT INTO `tt` VALUES ( `20040318140523` );

    Alternatively, in stead of using Date() in javascript, one could use
    MySQL's date functions, which might be experienced as more terse and
    powerful:

    http://dev.mysql.com/doc/en/Date_and_time_functions.html

    --
    Bart
     
    Bart Van der Donck, Mar 6, 2007
    #3
  4. Lee Guest

    Bart Van der Donck said:
    >
    >Lee wrote:
    >
    >> said:
    >>> im trying to convert Date() into a unix timestamp so i can
    >>> stick the result into a mysql db, please help!

    >>
    >> I've been using Unix since before you were born (I can tell by
    >> the fact that you haven't learned to use the SHIFT key), and
    >> have no idea what you mean by a "unix timestamp"

    >
    >Then you should definitely start learning about it!
    >
    > http://en.wikipedia.org/wiki/Unix_Timestamp
    > http://www.unixtimestamp.com


    I've never heard Epoch time called "Unix timestamp". Maybe the
    term is popular with non-Unix users?

    It's also obviously, from your further description, not what
    the OP meant.


    --
     
    Lee, Mar 6, 2007
    #4
  5. Lee wrote:

    > I've never heard Epoch time called "Unix timestamp". Maybe the
    > term is popular with non-Unix users?


    With Unix users in the first place, I'ld say...

    > It's also obviously, from your further description, not what
    > the OP meant.


    The original poster wants to insert a timestamp in MySQL. The
    recommended way to do that, is to use MySQL's TIMESTAMP data type,
    which is intended especially for that purpose. MySQL knows the
    UNIX_TIMESTAMP() function for quick&easy over-and-back conversions
    between Unix' and MySQL's timestamps.

    --
    Bart
     
    Bart Van der Donck, Mar 6, 2007
    #5
  6. Lee Guest

    Bart Van der Donck said:
    >
    >Lee wrote:
    >
    >> I've never heard Epoch time called "Unix timestamp". Maybe the
    >> term is popular with non-Unix users?

    >
    >With Unix users in the first place, I'ld say...


    Unix users don't call "ls" "the Unix ls command". They call it "ls".
    Similarly, Unix users (the many that I know, at least) aren't likely
    to call epoch time "the Unix timestamp".

    If there's a mysql function called UNIX_TIMESTAMP(), then there's
    a pretty good clue as to where the term originated.


    --
     
    Lee, Mar 6, 2007
    #6
  7. In comp.lang.javascript message <
    oglegroups.com>, Tue, 6 Mar 2007 08:24:03, ""
    <> posted:

    >im trying to convert Date() into a unix timestamp so i can stick the
    >result into a mysql db, please help!


    In Javascript, Date() gives a string, and of undefined format (ISO 16262
    15.9.2 &c). It's not a good starting-point for most requirements.

    But new Date() gives a Date Object, and so + new Date() gives
    the number of milliseconds (ignoring Leap Seconds, but not Summer Time)
    since the Javascript Epoch. Converting that to the number of seconds
    since the UNIX Epoch can be left as an exercise.

    If you want a MySQL format from a Javascript group, you should give its
    definition. Does it require to be in UTC or in local, with/without
    offset in what form? In any case, reading the newsgroup FAQ should put
    you in the right direction.

    If you require a string YYYYMMDDhhmmss, it's easiest to use, in a string
    context, Y*1e10 + M*1e8 + D*1e6 + h*1e4 + m*1e2 + s*1e0 .


    BTW, in the FAQ, it may become desirable to convert from ISBN-10 to the
    current ISBN-13 : see <URL:http://www.merlyn.demon.co.uk/js-misc0.htm>.

    It's a good idea to read the newsgroup and its FAQ. See below.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
    news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    <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 J R Stockton, Mar 6, 2007
    #7
    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. Brian Candy
    Replies:
    2
    Views:
    1,182
    Janaka
    Feb 18, 2004
  2. islamelnaggar
    Replies:
    4
    Views:
    3,007
    islamelnaggar
    Feb 24, 2008
  3. islamelnaggar
    Replies:
    0
    Views:
    496
    islamelnaggar
    Feb 24, 2008
  4. Michael Torrie
    Replies:
    0
    Views:
    402
    Michael Torrie
    Mar 12, 2010
  5. Andyza
    Replies:
    6
    Views:
    1,414
    Evertjan.
    Mar 5, 2009
Loading...

Share This Page