Solved

convert mm/dd/YYYY to timestamp in javascript

Posted on 2004-10-06
5
1,918 Views
Last Modified: 2012-08-14
How do I convert a date in mm/dd/YYYY format to unix timestamp?
0
Comment
Question by:skiboy825
  • 2
  • 2
5 Comments
 
LVL 25

Expert Comment

by:devic
ID: 12243742
<script>
function toUnixStamp(str)
{
    var s=str.split("/");
      if(s.length>1)return (new Date(Date.UTC(s[2],s[0],s[1],0,0,0)).getTime()/1000.0);
}
</script>
<button onclick=alert(toUnixStamp("10/22/2004"))>convert 10/22/2004 to unixstamp</button>
0
 

Author Comment

by:skiboy825
ID: 12243886
thank you, but that function appears to return the next month minus one day.
0
 
LVL 25

Accepted Solution

by:
devic earned 100 total points
ID: 12243913
ah I sleep already :)
=====================
<script>
function toUnixStamp(str)
{
   var s=str.split("/");
    if(s.length>1)return (new Date(Date.UTC(s[2],(s[0]*1-1),s[1],0,0,0)).getTime()/1000.0);
}
</script>
<button onclick=alert(toUnixStamp("10/22/2004"))>convert 10/22/2004 to unixstamp</button>
0
 

Author Comment

by:skiboy825
ID: 12243966
Thanks, works good.

function toUnixStamp(str)
{
   var s=str.split("/");
   if(s.length>1)return (new Date(Date.UTC(s[2],(s[0]*1-1),s[1]*1+1,0,0,0)).getTime()/1000.0);
}
0
 

Expert Comment

by:Reledsupport
ID: 37930380
I don't know why, but that function ended up giving me the timestamp for the day before the date I entered. I changed it to fix the problem, but I still don't know why it gave me the wrong day:
function toUnixStamp(str) // Converts mm/dd/yyyy format to Unix timestamp
{
   var s=str.split("/");
    if(s.length>1)return (new Date(Date.UTC(s[2],(s[0]*1-1),(s[1]*1+1),0,0,0)).getTime()/1000.0);
};

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question