I would like to give my users the ability to select the Swatch Internet Time as their time format.
How I'm trying right now:
function startTime(){
var d = new Date(),
h = d.getHours(),
m = d.getMinutes(),
s = d.getSeconds(),
time = "@"+parseFloat((h*3600+m*60+s*1)*0.01157,10).toString().replace(/(\.\d{1,2})\d*$/, "$1");
$('#time').html(time);
}
According to the description, the swatch time is timezone offset free, so using the user's system time won't work.
The other way I could think of is sending an ajax request to a php file which returns the current swatch time via the built-in date('B')
function, but then I cannot use 2 decimals, and also It would generate unnecessary traffic.
$.ajax({type:'GET',url:'../time.php'}).done(function(data){ var time = data });
time.php:
<?php echo date("B") ?>
There has to be some sort of way to return swatch time in a @###.##
format, and I'm interested to know.
The startTime()
function is called using setInterval(startTime,750)
, so I need a solution that can work in that time period.
I would like to give my users the ability to select the Swatch Internet Time as their time format.
How I'm trying right now:
function startTime(){
var d = new Date(),
h = d.getHours(),
m = d.getMinutes(),
s = d.getSeconds(),
time = "@"+parseFloat((h*3600+m*60+s*1)*0.01157,10).toString().replace(/(\.\d{1,2})\d*$/, "$1");
$('#time').html(time);
}
According to the description, the swatch time is timezone offset free, so using the user's system time won't work.
The other way I could think of is sending an ajax request to a php file which returns the current swatch time via the built-in date('B')
function, but then I cannot use 2 decimals, and also It would generate unnecessary traffic.
$.ajax({type:'GET',url:'../time.php'}).done(function(data){ var time = data });
time.php:
<?php echo date("B") ?>
There has to be some sort of way to return swatch time in a @###.##
format, and I'm interested to know.
The startTime()
function is called using setInterval(startTime,750)
, so I need a solution that can work in that time period.
- FYI: yes Swatch as in the watch maker... en.wikipedia/wiki/Swatch_Internet_Time – ficuscr Commented Feb 20, 2013 at 19:19
- @ficuscr It's already linked in the question... – SeinopSys Commented Feb 20, 2013 at 19:20
- What about the old dear timestamp? Damn marketing – Damien Pirsy Commented Feb 20, 2013 at 19:21
- 1 This is just a marketing ploy, there are no standardized functions, converters or API's for this, and time is divided into beats, wich uses the decimal system, and not 60 seconds per minute, 60 minutes per hour etc. which makes it a helluva lot of work to make something that really works. – adeneo Commented Feb 20, 2013 at 19:21
- 2 how about using this? themactep./beats/js – John Boker Commented Feb 20, 2013 at 19:32
3 Answers
Reset to default 5JavaScript has a function .getTimezoneOffset()
which returns the offset in minutes from GMT.
Just take the Biel, Switzerland time zone offset (+60 minutes) and subtract the local time zone offset. The difference should be the offset to give you the Swatch time. Then do the beat calculation based on that.
You can also just use .getUTCHours()
, .getUTCMinutes()
, etc, which is basically GMT. GMT is one hour before Biel, Switzerland. So, you can simply add an hour to your calculations from there to get Swatch time.
Demo: http://jsfiddle/qF8Bt/
I just had a reason to write a swatch beat function so will post here for posterity:
function getSwatchBeat(date, places = 3) {
return (((+date + 3.6e6) % 8.64e7) / 8.64e4).toFixed(places);
}
let d = new Date();
console.log(`Current swatch beat for ${d.toLocaleString()} is ${getSwatchBeat(d, 1)}`);
It just adds 1 hour to UTC time to adjust to Biel, then divides by 1 thousandth of a day. I don't know how many decimal places the PHP function returns, but that's easily adjusted.
I'd like to make the solution which @JohnBoker posed in the ments more visible. A JavaScript Date class extension written by Paul Philippov, which I've been using to this day. The method takes a single parameter which specifies the amount of decimals to return.
function startTime(){
var d = new Date(),
time = d.toInternetTime(2);
$('#time').html(time);
}
setInterval(startTime,500);
Try it out below:
function startTime() {
var d = new Date(),
time = d.toInternetTime(2);
$('#time').html(time);
}
setInterval(startTime, 500)
body{margin:0}#time{font-family:monospace;display:block;width: 100%;font-size:2em;height:2em;display:block;text-align:center;position:absolute;top:calc(50% - 1em)}
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://themactep./js/ppds.date.js"></script>
<span id=time></span>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744234998a4564430.html
评论列表(0条)