tastro Posted March 22, 2012 Share Posted March 22, 2012 hi, also i want to convert this date 12.12.2012 into seconds. in php i can do it with mktime() but no idea how to do that with javascript. i already googled like crazy but couldn't find anything helpful. please help. thank you, tastro Quote Link to comment https://forums.phpfreaks.com/topic/259518-convert-date-into-seconds-with-javascript/ Share on other sites More sharing options...
Adam Posted March 22, 2012 Share Posted March 22, 2012 Take a look at the JavaScript Date object. You can pass in parts of the date string into the constructor, similar to how you would with mktime(). Quote Link to comment https://forums.phpfreaks.com/topic/259518-convert-date-into-seconds-with-javascript/#findComment-1330312 Share on other sites More sharing options...
tastro Posted March 22, 2012 Author Share Posted March 22, 2012 this one doesn't work. :S with this: <script type="text/javascript"> /*year/month/date*/ var test = new Date(1995,11,17); document.write(test); </script> i get this: Sun Dec 17 1995 00:00:00 GMT+0100 (Central Europe Standard Time) but i need the outcome time in seconds. by seconds i mean UNIX timestamp. Quote Link to comment https://forums.phpfreaks.com/topic/259518-convert-date-into-seconds-with-javascript/#findComment-1330314 Share on other sites More sharing options...
Adam Posted March 22, 2012 Share Posted March 22, 2012 By using the object in a string context (document.write()) you're invoking the toString() method automatically. You need to call getTime() to get the milliseconds since the UNIX epoch: document.write(test.getTime()); Also if you want the time in seconds, you need to divide it by 1000: document.write(test.getTime() / 1000); Edit: I originally said multiply, it's divide, of course Quote Link to comment https://forums.phpfreaks.com/topic/259518-convert-date-into-seconds-with-javascript/#findComment-1330317 Share on other sites More sharing options...
tastro Posted March 22, 2012 Author Share Posted March 22, 2012 the problem is that i don't need the time now. i need the to convert a specific date into the unix timestamp for example: 12.12.2012 or 10.10.2011 Quote Link to comment https://forums.phpfreaks.com/topic/259518-convert-date-into-seconds-with-javascript/#findComment-1330318 Share on other sites More sharing options...
Adam Posted March 22, 2012 Share Posted March 22, 2012 The 'UNIX timestamp' is basically the number of seconds since 01/01/1970? Quote Link to comment https://forums.phpfreaks.com/topic/259518-convert-date-into-seconds-with-javascript/#findComment-1330320 Share on other sites More sharing options...
tastro Posted March 22, 2012 Author Share Posted March 22, 2012 yes exactly that's the unix timestamp. sry for the reply before... i didn't even see that you already provided the answer to me. thanks mate! it's working perfect! <script type="text/javascript"> //year/month/date var time_then = new Date(1995,11,17); document.write(time_then.getTime()); var time_now = new Date(); document.write('<br /> '+time_now.getTime()); </script> one more thing... i'm new to javascript, but i'm pretty good at php (i think) how should i write variables in my code? like this: var time_now = new Date(); var time_now = time_now.getTime(); like this: var time_now = new Date(); time_now = time_now.getTime(); or like this? time_now = new Date(); time_now = time_now.getTime(); Quote Link to comment https://forums.phpfreaks.com/topic/259518-convert-date-into-seconds-with-javascript/#findComment-1330323 Share on other sites More sharing options...
tastro Posted March 22, 2012 Author Share Posted March 22, 2012 i have solved this now. thank you! Quote Link to comment https://forums.phpfreaks.com/topic/259518-convert-date-into-seconds-with-javascript/#findComment-1330330 Share on other sites More sharing options...
Adam Posted March 22, 2012 Share Posted March 22, 2012 Great stuff! It's just basic math really, once you've get the millisecond difference between the two dates. Also to answer your second question, you only need to declare the variable once with var, after that you can just redefine it like in your second example. One thing to note about JS compared with PHP is that variables declared in the global scope are automatically available within functions, unless you declare a local copy with var. Quote Link to comment https://forums.phpfreaks.com/topic/259518-convert-date-into-seconds-with-javascript/#findComment-1330335 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.