使用 jQuery/Javascript 获取上周日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12805981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:30:54  来源:igfitidea点击:

Get last week date with jQuery/Javascript

javascriptjquery

提问by streetlight

I'm trying to get the last week date in javascript, without the time.

我试图在没有时间的情况下在 javascript 中获取上周的日期。

So for example, 10-02-2012, instead of 10-02-12 13:34:56 GMT.

例如,10-02-2012,而不是 10-02-12 13:34:56 GMT。

Is there an easy solution out there for this?

有没有一个简单的解决方案?

Thank you!

谢谢!

Edit:

编辑:

I'm trying to make this dynamic, so that the resulting variable is always one week before the current date. Here's what I've done to calculate the today variable, if this helps or can be used!

我试图使这个动态,以便结果变量总是在当前日期前一周。这是我为计算今天变量所做的工作,如果这有帮助或可以使用的话!

 var currentTime = new Date();
                            var month = currentTime.getMonth() + 1
                            var day = currentTime.getDate();
                            var year = currentTime.getFullYear();
                            var today = month + "-" + day + "-" + year;
                            alert(today)

回答by CaffGeek

I prefer something like this ?

我更喜欢这样的东西?

function getLastWeek() {
  var today = new Date();
  var lastWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 7);
  return lastWeek;
}

var lastWeek = getLastWeek();
var lastWeekMonth = lastWeek.getMonth() + 1;
var lastWeekDay = lastWeek.getDate();
var lastWeekYear = lastWeek.getFullYear();

var lastWeekDisplay = lastWeekMonth + "/" + lastWeekDay + "/" + lastWeekYear;
var lastWeekDisplayPadded = ("00" + lastWeekMonth.toString()).slice(-2) + "/" + ("00" + lastWeekDay.toString()).slice(-2) + "/" + ("0000" + lastWeekYear.toString()).slice(-4);

console.log(lastWeekDisplay);
console.log(lastWeekDisplayPadded);

And if you're using jQuery UI, you can do this instead of the manual steps to build the string

如果您使用的是 jQuery UI,则可以执行此操作而不是手动步骤来构建字符串

var lastWeekDisplay = $.datepicker.formatDate('mm/dd/yy', getLastWeek());

Or for today

或者今天

var todayDisplay = $.datepicker.formatDate('mm/dd/yy', new Date());

回答by Rahul Tripathi

 var firstDay = new Date("2009/10/02");
 var previousweek= new Date(firstDay.getTime() - 7 * 24 * 60 * 60 * 1000);

Check out this link. It will help:- http://code.google.com/p/datejs/

看看这个链接。它会有所帮助:- http://code.google.com/p/datejs/

回答by u.k

We can't have a javascript date question answered without mentioning Moment.js.

我们无法在不提及Moment.js 的情况下回答 javascript 日期问题。

moment().subtract('days', 7).format('MM-DD-YYYY')

回答by Haythem Farhat

You can simply do

你可以简单地做

new Date().setDate(new Date().getDate() - 7)

回答by Zubair

use this code to subtract any number of days as i have selected 9 it will give last 10 days result including today

使用此代码减去任意天数,因为我选择了 9,它将给出包括今天在内的过去 10 天的结果

var date = new Date();
    var day=date.getDate();
    var month=date.getMonth() + 1;
    var year=date.getFullYear();
    var startDate=day+"/"+month+"/"+year;
    var dayBeforeNineDays=moment().subtract(9, 'days').format('DD/MM/YYYY');
    startDate=dayBeforeNineDays;
    var endDate=day+"/"+month+"/"+year;

回答by Bastiaan ten Klooster

If you really want to create this from a full timestamp like 10-02-12 13:34:56 GMT, you might want to do this:

如果您真的想从格林威治标准时间 10-02-12 13:34:56 这样的完整时间戳创建它,您可能想要这样做:

var time = '10-02-12 13:34:56 GMT';
document.write(time.substr(0,7));