根据星期几asp.net mvc 5指定当前周

时间:2016-03-08 09:05:56

标签: asp.net asp.net-mvc

目前,我正在尝试根据一周中的某一天从该周的数据库中获取电影。 所以电影周是从周四到下周三。 星期天,我只想要周日到周三的电影。 如果是星期二,我只想要周二到周三的电影等。 我真的陷入了你能做什么,不能做任何建议吗?

到目前为止,这是我的代码:

public IEnumerable<Viewing> GetUpcomingWeekViews()
    {
        var viewingList1 = _efdbContext.Viewing.ToList();

        DateTime currentDate = DateTime.Now.Date;
        var viewingList = _efdbContext.Viewing.ToList();

        DateTime startOfWeek = DateTime.Now.

        return viewingList.Where(v => (
            v.StartTime.Date == currentDate.Date) &&
            (v.StartTime.TimeOfDay > currentDate.TimeOfDay)
        ).OrderBy(v => v.StartTime);
    }

2 个答案:

答案 0 :(得分:0)

使用 private void copyFile(AppDB apkfile) { ProgressDialog pd=new ProgressDialog(this,ProgressDialog.STYLE_SPINNER); pd.show(this,"Coping ...",apkfile.name,true,true); File f1 = new File(apkfile.location); try { String fileName = apkfile.name; File f2 = new File(Environment.getExternalStorageDirectory().toString() + "/" + "Easy Share"); f2.mkdirs(); f2 = new File(f2.getPath() + "/" + fileName + ".apk"); f2.createNewFile(); InputStream in = new FileInputStream(f1); OutputStream out = new FileOutputStream(f2); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); } catch (FileNotFoundException ex) { Toast.makeText(this, ex.getMessage() + " in the specified directory.", Toast.LENGTH_SHORT); } catch (IOException e) { Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT); } if(pd.isShowing()) { pd.dismiss();} } 的{​​{1}}属性:

DayOfWeek

Fiddle

答案 1 :(得分:0)

首先使用DateOfWeek计算从当前日期到所需日期的偏移量

public int CalculateOffset(DayOfWeek current, DayOfWeek desired) {
    // f( c, d ) = [7 - (c - d)] mod 7
    // f( c, d ) = [7 - c + d] mod 7
    // c is current day of week and 0 <= c < 7
    // d is desired day of the week and 0 <= d < 7
    int c = (int)current;
    int d = (int)desired;
    int offset = (7 - c + d) % 7;
    return offset == 0 ? 7 : offset;
}

并将该偏移量添加到当前Date

DateTime
public IEnumerable<Viewing> GetUpcomingWeekViews(DayOfWeek desiredDayOfWeek = DayOfWeek.Thursday) {
    DateTime minStartTime = DateTime.Now;
    var currentDayOfWeek = minStartTime.DayOfWeek;

    int offset = CalculateOffset(currentDayOfWeek, desiredDayOfWeek);

    DateTime maxStartTime = minStartTime.Date.AddDays(offset);

    var viewingList = dbContext.Viewing.ToList();

    return viewingList
        .Where(v => minStartTime <= v.StartTime && v.StartTime < maxStartTime)
        .OrderBy(v => v.StartTime);
}