简体   繁体   English

如何将从getTime()方法接收的日期转换为DD-MM-YYYY格式?

[英]How to convert date received from getTime() method to DD-MM-YYYY format?

My problem is simple yet so hard for me to solve. 我的问题很简单,但我却很难解决。 Basically I am storing the number received from the new Date().getTime() method in the database and then using that string in a time ago function. 基本上,我将从new Date().getTime()方法接收的数字存储在数据库中,然后在以前的函数中使用该字符串。 The function works very well but I don't know how to convert the string back to a DD-MM-YYYY ; h:m:s 该函数效果很好,但是我不知道如何将字符串转换回DD-MM-YYYY ; h:m:s DD-MM-YYYY ; h:m:s format DD-MM-YYYY ; h:m:s格式

my time ago function: 我以前的功能:

<script>
$(document).ready(function() {
var db_time = //the string from the database;
var c_db_time = db_time/1000;//remove the milli seconds
function ago()
{
    var current_time = new Date().getTime()/1000;//remove the milli seconds
    var dif = Math.round(current_time - c_db_time);
    if(dif<60)//one minute ago
    {
        if(dif<=10){$(".elapsed_time").html("just now");}
        else{var ago = dif;$(".elapsed_time").html(ago+"sec ago");}
    }

    else if(dif<3600)//one hour ago
    {
        var ago = Math.round(dif/60);
        $(".elapsed_time").html(ago+"min ago")
    }
    else if(dif<86400)//one day ie.24hours ago
    {
        var ago = Math.round(dif/3600);
        $(".elapsed_time").html(ago+"hr ago");
    }
    else if(dif<604800)// one week ago
    {
        var ago = Math.round(dif/86400);
        $(".elapsed_time").html(ago+"Day ago");
    }

}
setInterval(ago,1000);//run the script every 1 sec
});
</script>

Note: I researched everywhere on google and stack overflow but could not find an answer that works for me. 注意:我在Google上到处都在研究,堆栈溢出,但是找不到适合我的答案。

Moment.js is a great library for doing things like this. Moment.js是一个出色的库,可用于执行此类操作。

moment(theDate).format('dd-mm-yyyy ; h:m:s');

It also supports getting dates back from strings: 它还支持从字符串获取日期:

moment('01-01-2001', 'dd-mm-yyyy');

Moment also has an extremely robust handling of the relative time statements you're doing manually: Moment对您手动执行的相对时间语句也具有非常强大的处理能力:

moment(theDate).fromNow(); // 11 hours ago, in 3 days, etc.

Working with native date objects in Javascript can get tedious. 使用Javascript中的本机日期对象可能会很乏味。 Date math in general is hard. 一般而言,日期数学很难。 Moment wraps up all the hard work and lets you just describe what you want. Moment总结了所有艰苦的工作,并让您仅描述您想要的内容。

You can construct a Date instance from a timestamp value and then use the various accessor methods ( .getDate() , .getMonth() , .getFullYear() etc) to format the date according to your needs. 您可以根据时间戳值构造Date实例,然后使用各种访问器方法( .getDate() .getMonth() .getFullYear()等)来根据需要格式化日期。

So for example: 因此,例如:

var savedDate = new Date(db_time);

to make a Date instance. 制作一个Date实例。 Then it's helpful to have a function to produce two-digit zero-prefixed numbers: 然后,具有产生两位数零前缀数字的功能将很有帮助:

function d2( n ) {
  return (n < 10 ? "0" : "") + n;
}

Then a DD-MM-YYYY date can be built: 然后可以建立一个DD-MM-YYYY日期:

var ddmmyy = d2(savedDate.getDate()) + "-" + d2(savedDate.getMonth() + 1) + "-" + savedDate.getFullYear();

(The month values from Date instances run 0 to 11, thus the need to add 1.) (“日期”实例中的月份值从0到11,因此需要加1。)

The javascript Date object has a toString() method, which will return a string representation of that date. javascript Date对象具有toString()方法,该方法将返回该日期的字符串表示形式。 However, if you aren't happy with the formatting of that string, there's no way to specify a specific format to use for the string -- you'd have to start pulling out the pieces of the date and building a string yourself. 但是,如果您对该字符串的格式不满意,则无法指定用于该字符串的特定格式-您必须开始提取日期的各个部分并自己构建一个字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM