Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

convert mm/dd/YYYY to timestamp in javascript

Posted on 2004-10-06
5
Medium Priority
?
2,195 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 400 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

783 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