te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - date.setHours() not working - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - date.setHours() not working - Stack Overflow

programmeradmin3浏览0评论

I am trying to subtract hours from a given date time string using javascript. My code is like:

     var cbTime = new Date();    
     cbTime = selectedTime.setHours(-5.5);

Where selectedTime is the given time (time that i pass as parameter).

So suppose selectedTime is Tue Sep 16 19:15:16 UTC+0530 2014 Ans I get is : 1410875116995

I want answer in datetime format. Am I doing something wrong here? Or there is some other solution?

I am trying to subtract hours from a given date time string using javascript. My code is like:

     var cbTime = new Date();    
     cbTime = selectedTime.setHours(-5.5);

Where selectedTime is the given time (time that i pass as parameter).

So suppose selectedTime is Tue Sep 16 19:15:16 UTC+0530 2014 Ans I get is : 1410875116995

I want answer in datetime format. Am I doing something wrong here? Or there is some other solution?

Share Improve this question asked Sep 17, 2014 at 9:47 user1181942user1181942 1,6077 gold badges37 silver badges46 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

The reason is that setHours(), setMinutes(), etc, take an Integer as a parameter. From the docs:

...

The setMinutes() method sets the minutes for a specified date according to local time.

...

Parameters:

An integer between 0 and 59, representing the minutes.

So, you could do this:

var selectedTime = new Date(),
    cbTime = new Date(); 
   
cbTime.setHours(selectedTime.getHours() - 5);
cbTime.setMinutes(selectedTime.getMinutes() - 30);

document.write('cbTime: ' + cbTime);
document.write('<br>');
document.write('selectedTime: ' + selectedTime);

Well first off setting the hours to -5.5 is nonsensical, the code will truncate to an integer (-5) and then take that as "five hours before midnight", which is 7PM yesterday.

Second, setHours (and other functions like it) modify the Date object (try console.log(cbTime)) and return the timestamp (number of milliseconds since the epoch).

You should not rely on the output format of the browser converting the Date object to a string for you, and should instead use get*() functions to format it yourself.

According to this:

http://www.w3schools./jsref/jsref_sethours.asp

You'll get "Milliseconds between the date object and midnight January 1 1970" as a return value of setHours.

Perhaps you're looking for this:

http://www.w3schools./jsref/tryit.asp?filename=tryjsref_sethours3

Edit: If you want to subtract 5.5 hours, first you have to subtract 5 hours, then 30 minutes. Optionally you can convert 5.5 hours to 330 minutes and subtract them like this:

var d = new Date();
d.setMinutes(d.getMinutes() - 330);
document.getElementById("demo").innerHTML = d;

Use:

  var cbTime = new Date();
        cbTime.setHours(cbTime.getHours() - 5.5)
        cbTime.toLocaleString();

try this:

 var cbTime = new Date();
    cbTime.setHours(cbTime.getHours() - 5.5)
    cbTime.toLocaleString();
发布评论

评论列表(0)

  1. 暂无评论