Solved

convert mm/dd/YYYY to timestamp in javascript

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

746 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

12 Experts available now in Live!

Get 1:1 Help Now