如何通过使用内置的Date类节省时间? [英] How can you save time by using the built in Date class?

查看:248
本文介绍了如何通过使用内置的Date类节省时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题的目的是使用,而不是写长复杂的功能内置的Date类收集解决方案,日期/时间计算。

The intention of this question is to gather solutions to date / time calculation using the built in Date class instead of writing long complicated functions.

我会写一些自己的答案,并接受一个答案,如果有人想出了一些非常聪明的。但是,这主要是指作为解决方案的集合,因为我经常看到过于复杂code来处理日期。

I’ll write some answers myself, and accept an answer if anyone comes up with something very clever. But this is mostly meant as a collection of solutions, since I often see overly complicated code for handling dates.

请记住这是不会作为长期解决方案的事Date类不能做。

Please remember this is not for long solutions for things the Date class can not do.

有一个良好的开端是在这里发现的参考: http://help.adobe.com/en_US/AS3LCR /Flash_10.0/Date.html

A good place to start is the reference found here: http://help.adobe.com/en_US/AS3LCR/Flash_10.0/Date.html

推荐答案

要正确比较的约会,你需要使用的getTime()函数,它会给你以毫秒为单位的时间,因为1月1日,1970年这使得很容易要比较的日期,以后的日子会返回一个较大的值。

To properly compare to dates you need to use the getTime() function, it will give you the time in milliseconds since January 1, 1970. Which makes it easy to compare to dates, a later date will return a larger value.

您可以减去从另一个拿到的差异,但遗憾的是没有内置的时间跨度类来处理这个干净;你必须正确地使用一点数学为present的差异给用户(例如,除以数毫秒的差异在一天之内得到天的差异)。

You can subtract one from the other to get the difference, but unfortunately there is no built in time span class to handle this cleanly; you will have to use a bit of math to present the difference properly to the user (eg. dividing the difference with the number milliseconds in a day to get the difference in days).

var date1:Date = new Date(1994, 12, 24);
var date2:Date = new Date(1991, 1, 3);
if(date1.getTime() > date2.getTime())
    trace("date1 is after date2");
else
    trace("date2 is after or the same as date1");

这篇关于如何通过使用内置的Date类节省时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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