如何使用C#中的日期时间获取月份的第一天和月份的最后一天

时间:2014-04-17 04:44:56

标签: c#

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脚本的标准书

,我将很高兴

5 个答案:

答案 0 :(得分:2)

请尝试使用以下代码段。

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

答案 1 :(得分:0)

var d = DateTime.Today;
// Start
d.AddDays(-d.Day+1);
// End
d.AddMonths(1).AddDays(-d.Day).Dump();

答案 2 :(得分:0)

使用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

答案 3 :(得分:0)

要获得每月的第一天:

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();

答案 4 :(得分:0)

 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());