SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question beginner question on date and time

    Dear all,

    I'm working on a calendar system of my order system where the time of the calendar must 6 hours faster then actual time. I need this to be set as the order needs at least 6 hours to process. So when user place an order after 6.00pm, he can only select the next day for the delivery.

    I've tried below script.

    <script type="text/javascript">
    var six_hours_later = some_claculation
    var myDate=new Date();
    var final = myDate.setTime(myDate.getTime()+six_hours_later);
    document.write(final);
    </script>
    However, when i try to document.write(final);
    It display in time format eg. 1226979280911
    May I know how can I display it in date format dd/MM/yyyy ?

    Between, do I get the concept correct?

  2. #2
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    found the solution...

    <script type="text/javascript">
    var d=new Date();
    d.setTime(d.getTime()+21600000);

    var month = d.getMonth() + 1
    var day = d.getDate()
    var year = d.getFullYear()
    document.write(day + "/" + month + "/" + year)


    </script>

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    It's also possible to use the toDateString method which returns a string looking like:
    Wed Nov 19 2008
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    pmw57,

    Thanks for your suggestion!
    Is it possible to format the date string to dd/MM/yyyy ?

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Not directly with the built-in methods, but some clever sod has create a javascript date format method for the date object so that you can specify whichever formatting options that you like.

    Code javascript:
    var formattedDate = d.format('dd/mm/yyyy');
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •