Solved

convert mm/dd/YYYY to timestamp in javascript

Posted on 2004-10-06
5
1,904 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now