查找去年同一天的日期? [英] Find date for same day of the week last year?

查看:196
本文介绍了查找去年同一天的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK,例如今天是02月02日星期二。与去年相当的星期二是2月03日。

OK so for example, today is Tuesday, Feb 02. Well the equivalent "Tuesday" from last year was on Feb 03.

我如何以编程方式找到这个?

How can I find this out programmatically?

谢谢!!

推荐答案

只需我的两分钱。我不喜欢这个想法,第二个答案假设一年52周,它将工作一年,但只是解决这个确切的问题 - 例如。如果你想检查相同的事情回溯10年,它将无法工作。我会这样做:

Just my two cents. I don't like the idea, that the second answer assumes 52 weeks in a year, it will work for a single year, but is a solution to only this exact problem - eg. if you want to check the same thing moving back 10 years it won't work. I'd do it like this:

var today:Date = new Date();    
// Here we store the day of the week            
var currentDay:int = today.day;
trace (today);      
const milisecondsInADay:uint = 1000*60*60*24;
// Here we move back a year, but we can just as well move back 10 years
// or 2 months      
today.fullYear -= 1;
// Find the closest date that is the same day of the week as current day
today.time -= milisecondsInADay*(today.day-currentDay);     
trace (today);

返回:

Tue Feb 2 21:13:18 GMT+0100 2010 
Tue Feb 3 21:13:18 GMT+0100 2009

这篇关于查找去年同一天的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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