如何不花时间获取当前日期? [英] How to get current date without time?

查看:87
本文介绍了如何不花时间获取当前日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取不带时间的当前日期,并将其存储在JavaScript中的变量中.它需要没有时间,因为我将其转换为一个纪元日期,我将用它来度量过去的24小时(如果日期在24小时之内,那么它将显示出来).问题在于,随着时间的增加,它与过去24小时之内不匹配.

I'm trying to get the current date without the time and store it in a variable, within JavaScript. It needs to be without time as I'm converting it to an epoch date, with which I will use to measure the past 24 hours (if date is within 24 hours then it will be displayed). The problem is that with the added time, it doesn't match as within the last 24 hours.

例如转换为纪元时,它将返回以下日期:1408704590485

e.g. it returns the date as the following when converted to epoch: 1408704590485

我希望它像1408662000000

I want it to be like 1408662000000

我不确定该怎么做.

代码-当前日期当前日期是如何存储的-

Code - How the current days epoch date is currently being stored -

var epochLoggingFrom;
var epochLoggingTo;

$(document).ready(function () {
    epochLoggingFrom = dateToEpoch(new Date());
    epochLoggingTo = dateToEpoch(new Date());
}

dateToEpoch函数-

dateToEpoch function -

function dateToEpoch(thedate) {
    return thedate.getTime();
}

推荐答案

尝试一下:

function dateToEpoch(thedate) {
    var time = thedate.getTime();
    return time - (time % 86400000);
}

或者这个:

function dateToEpoch2(thedate) {
   return thedate.setHours(0,0,0,0);
}

示例: http://jsfiddle.net/chns490n/1/

参考:(数字)Date.prototype.setHours(小时,分钟,秒,毫秒)

这篇关于如何不花时间获取当前日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆