如何使用C#中的日期时间获取月的第一天和最后一天
本文关键字:第一天 最后一天 获取 日期 何使用 时间 | 更新日期: 2023-09-27 17:58:53
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class calenderdisp : System.Web.UI.Page
{
DateTime dt = DateTime.Now;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack )
{
if (ddlweeklist.SelectedItem.Text == "Today")
{
txtstart.Text = DateTime.Now.ToString("dd/MM/yyyy");
Txtend.Text = DateTime.Today.ToString("dd/MM/yyyy");
}
}
}
protected void ddlweeklist_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlweeklist.SelectedItem.Text == "This Week")
{
int st = DayOfWeek.Sunday - dt.DayOfWeek;
int en = DayOfWeek.Saturday - dt.DayOfWeek;
txtstart.Text = dt.AddDays(st).ToString("dd/MM/yyyy");
Txtend.Text = dt.AddDays(en).ToString("dd/MM/yyyy");
}
if (ddlweeklist.SelectedItem.Text == "Next Week")
{
DateTime dt1 = dt.AddDays(7);
int st = DayOfWeek.Sunday - dt1.DayOfWeek;
int en = DayOfWeek.Saturday - dt1.DayOfWeek;
txtstart.Text = dt1.AddDays(st).ToString("dd/MM/yyyy");
Txtend.Text = dt1.AddDays(en).ToString("dd/MM/yyyy");
}
if (ddlweeklist.SelectedItem.Text == "Last Week")
{
DateTime dt2 = dt.AddDays(-7);
int st = DayOfWeek.Sunday - dt2.DayOfWeek;
int en = DayOfWeek.Saturday - dt2.DayOfWeek;
txtstart.Text = dt2.AddDays(st).ToString("dd/MM/yyyy");
Txtend.Text = dt2.AddDays(en).ToString("dd/MM/yyyy");
}
if (ddlweeklist.SelectedItem.Text == "This Month")
{
DateTime stmonth = new DateTime(dt.Year, dt.Month, 1);
DateTime enmnth = new DateTime(dt.Year, dt.Month, DateTime.DaysInMonth(dt.Year, dt.Month));
txtstart.Text = stmonth.ToString("dd/MM/yyyy");
Txtend.Text = enmnth.ToString("dd/MM/yyyy");
txtnodm.Text = DateTime.DaysInMonth(dt.Year, dt.Month).ToString();
Isleap();
}
}
}
现在得到了答案。。。谢谢你。。。。。。。。。但是如何使用java脚本来实现这一点,因为我非常有兴趣学习它,所以请建议用javascript 编写这段代码
如果你让我知道学习java脚本
请尝试使用以下代码片段。
DateTime dt = DateTime.Now; //Your Date
DateTime start = new DateTime(dt.Year, dt.Month, 1); //First Date of the month
DateTime end = start.AddMonths(1).AddDays(-1); //Last Date of the month
string startDay = start.DayOfWeek.ToString(); //First weekday of the month
string endDay = end.DayOfWeek.ToString(); //Last weekday of the month
var d = DateTime.Today;
// Start
d.AddDays(-d.Day+1);
// End
d.AddMonths(1).AddDays(-d.Day).Dump();
使用DateTime构造函数第一天很容易。最后一天比下个月的第一天少一天。
public static void FirstAndLastDayOfMonth(DateTime date, out DateTime first, out DateTime last) {
first = new DateTime(date.Year, date.Month, 1);
DateTime nextFirst;
if (first.Month == 12) nextFirst = new DateTime(first.Year + 1, 1, 1);
else nextFirst = new DateTime(first.Year, first.Month + 1, 1);
last = nextFirst.AddDays(-1);
}
你也可以这样得到最后一天:
DateTime today = DateTime.Today;
DateTime endOfMonth = new DateTime(today.Year, today.Month, DateTime.DaysInMonth(today.Year, today.Month));
或:
DateTime today = DateTime.Today;
DateTime endOfMonth = new DateTime(today.Year, today.Month, 1).AddMonths(1).AddDays(-1);
此链接也可能对您有所帮助:
http://www.c-sharpcorner.com/UploadFile/scottlysle/FirstAndLastDay10262007135750PM/FirstAndLastDay.aspx
DateTime dt = DateTime.Now.AddDays(-(DateTime.Now.Day - 1));
int lday= DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
DateTime DTY = new DateTime(DateTime.Now.Year, DateTime.Now.Month, lday);
MessageBox.Show("First Day :- "+dt.DayOfWeek.ToString());
MessageBox.Show("Last Day :-" + DTY.DayOfWeek.ToString());
获取月初一:
String day = System.DateTime.Now.DayOfWeek.ToString();
int date = System.DateTime.Now.Day;
String fdm = System.DateTime.Now.AddDays(-Convert.ToDouble(date - 1)).DayOfWeek.ToString();
获取月的最后一天:
var now = DateTime.Now;
var startOfMonth = new DateTime(now.Year, now.Month, 1);
var DaysInMonth = DateTime.DaysInMonth(now.Year, now.Month);
var lastDay = new DateTime(now.Year, now.Month, DaysInMonth);
string lastday = lastDay.DayOfWeek.ToString();