SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript date how to convert 'dd-MMMM-yyyy' format in timestamp ?

    I've got the current date from a datepicker widget in this format

    dd-MMMM-yyyy

    but all in all it could be a better format dd MMMM yyyy if possible

    how to convert it in timestamp ?

  2. #2
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I could map the month but it's quite ugly ^^

  3. #3
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,347
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    Hi there,

    Could you give me an example of what you have (e.g. "01-01-14) and what you want (e.g. "01 January 2014")

    Sorry if this is obvious, I'm having a slow day

  4. #4
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Hi there,

    Could you give me an example of what you have (e.g. "01-01-14) and what you want (e.g. "01 January 2014")

    Sorry if this is obvious, I'm having a slow day
    I've got

    26-January-2014

    I want a timestamp like

    1390690800000

    possible with the right timezone

    I found https://developer.mozilla.org/en-US/...cts/Date/parse
    but it doesn't work with that format

    or
    new Date("26 January 2014").getTime()

  5. #5
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,347
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    Hi,

    Just do this:

    Code:
    var date = new Date("26-January-2014"),
        timestamp = +date;
    
    console.log(timestamp);
    =>1390690800000

  6. #6
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Hi,

    Just do this:

    Code:
    var date = new Date("26-January-2014"),
        timestamp = +date;
    
    console.log(timestamp);
    =>1390690800000
    Code:
    var date = new Date("26-January-2014");
    +date;
    give me NaN

  7. #7
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,347
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    Shouldn't do.

    What does the following command give you:

    Code:
    console.log(Number(new Date("26-January-2014")));

  8. #8
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,524
    Mentioned
    52 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by whisher View Post
    Code:
    var date = new Date("26-January-2014");
    +date;
    give me NaN
    Take a closer look Pullo's code again. You didn't replicate the syntax properly.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  9. #9
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Force Flow View Post
    Take a closer look Pullo's code again. You didn't replicate the syntax properly.
    it's simply a shortcut

    I found the tricky point
    may be or sure my ff version it's buggy
    in the console also
    Code:
    Number(new Date("26-January-2014"))
    give me NaN
    I tried it in the chrome console and it works
    thanks for the snippet

  10. #10
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,347
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    Hi,

    It's the hyphens in the date that FF is balking at, Chrome seems to accept them for some reason.

    This will work in FF, too:

    Code:
    var date = new Date(("26-January-2014").replace(/-/g, " "));
    +date


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •