Timestamp converter


CFAbsoluteTime is the primary timestamp format in Mac OS X and iOS.

To convert a CFAbsoluteTime to a UNIX timestamp, you've got to subtract 978307200000, as CFAbsoluteTime is defined as the number of seconds since 2001-01-01 00:00:00 and the UNIX timestamp is defined as the number of seconds since 1970-01-01 00:00:00.

JavaScript uses the same reference date as UNIX, but gives the number of milliseconds, so the JS timestamp is the UNIX timestamp multiplied by 1000.


CFAbsoluteTime
UNIX timestamp
JS timestamp
Date US (middle-endian) representation mm/dd/yyyy hh:mm:ss



... as I was unable to find a simple CFAbsoluteTime converter in Javascript on the internet, I put this one together. This is the code. (required JQuery)


<p>CFAbsoluteTime: <input type="text" id="t_cf">
</p>

<p>UNIX timestamp: <input type="text" id="t_nix">
</p>

<p>JS timestamp: <input type="text" id="t_js">
</p>

<p>mm/dd/yyyy hh:mm:ss representation</p>
<p><input type="text" id="t_ami"></p>

<script>
$(function() {
  var DIFF=978307200000;
  function sett(d) {
    $("#t_cf").val((d.getTime()-DIFF)/1000);
    $("#t_nix").val(d.getTime()/1000);
    $("#t_js").val(d.getTime());
    $("#t_ami").val(d.toString());
  }
  $("#t_cf").keyup(function() { sett(new Date(((+$(this).val())*1000)+DIFF)); });
  $("#t_nix").keyup(function() { sett(new Date((+$(this).val())*1000)); });
  $("#t_js").keyup(function() { sett(new Date((+$(this).val()))); });
  $("#t_ami").keyup(function() { sett(new Date($(this).val())); });

});
</script>