Link to home
Start Free TrialLog in
Avatar of skiboy825
skiboy825

asked on

convert mm/dd/YYYY to timestamp in javascript

How do I convert a date in mm/dd/YYYY format to unix timestamp?
Avatar of devic
devic
Flag of Germany image

<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>
Avatar of skiboy825
skiboy825

ASKER

thank you, but that function appears to return the next month minus one day.
ASKER CERTIFIED SOLUTION
Avatar of devic
devic
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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);
}
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