4

有没有人有一个简单的代码来显示从网络服务器获取的周数、日期和时间?

然后是另一个从本地计算机获取时间的代码?

我正在寻找 JS 或 PHP 代码

4

1 回答 1

2

On server side you can use (PHP)

$currentWeekNumber = (int)date('w'); // ISO-8601 week number of the year
$date = date('H:i:s d-m-Y'); // for example (you can use different format)

JS implementation is not so trivial. There is no built-in function, but here is an example of what is done.

function getWeek (getdate) { 
        var a, b, c, d, e, f, g, n, s, w; 

        $y = getdate.getFullYear(); 
        $m = getdate.getMonth() + 1; 
        $d = getdate.getDate(); 

        if ($m <= 2) { 
            a = $y - 1; 
            b = (a / 4 | 0) - (a / 100 | 0) + (a / 400 | 0); 
            c = ((a - 1) / 4 | 0) - ((a - 1) / 100 | 0) + ((a - 1) / 400 | 0); 
            s = b - c; 
            e = 0; 
            f = $d - 1 + (31 * ($m - 1)); 
        } else { 
            a = $y; 
            b = (a / 4 | 0) - (a / 100 | 0) + (a / 400 | 0); 
            c = ((a - 1) / 4 | 0) - ((a - 1) / 100 | 0) + ((a - 1) / 400 | 0); 
            s = b - c; 
            e = s + 1; 
            f = $d + ((153 * ($m - 3) + 2) / 5) + 58 + s; 
        } 

        g = (a + b) % 7; 
        d = (f + g - e) % 7; 
        n = (f + 3 - d) | 0; 

        if (n < 0) { 
            w = 53 - ((g - s) / 5 | 0); 
        } else if (n > 364 + s) { 
            w = 1; 
        } else { 
            w = (n / 7 | 0) + 1; 
        } 

        $y = $m = $d = null; 

        return w; 
    } 

weeknumber = getWeek(new Date());

UPD: Today, momentjs is capable of many things regarding dates in JS.

于 2012-07-26T08:18:40.737 回答