0
 private void btntest_Click(object sender, EventArgs e)
    {
        DateTime d = new DateTime(2016,2,13);
        LunarDate ld1 = LunarYearTools.SolarToLunar(d);
        lblamlich.Text = (ld1.ToString());
    }

Note DateTime d = new DateTime(Int year,Int month,Int day)

How to insert datetimepicker's value into new DateTime(2016,2,13) ??? I tried use casting datetime to Int but it didn't work:

        string a = dateTimePicker1.Value.ToString();
        int b = (int)(a);
        DateTime d = new DateTime(b);
        LunarDate ld1 = LunarYearTools.SolarToLunar(d);
        lblamlich.Text = (ld1.ToString());
Jr.Kan
  • 15
  • 5

2 Answers2

0

If I have understood you correctly, it should be as simple as this:

var d = dateTimePicker1.Value;

var newD = new DateTime(d.Year, d.Month, d.Day);
oldcoder
  • 342
  • 2
  • 12
  • The `Value` is already a DateTime, if the goal is to get the day only (no hours) then `var dateOnly =dateTimePicker1.Value.Date;` should probably suffice – pinkfloydx33 Jun 17 '17 at 09:56
  • @pinkfloydx33: You are of course completely correct. However, that was not what the questioner asked. He specifically asked how to get the values into a new DateTime object. – oldcoder Jun 17 '17 at 13:37
  • Well since DateTime is a struct, assignment would "put them" into a new datetime object. My comment was mostly intended for the OP and probably should've been posted on the question – pinkfloydx33 Jun 17 '17 at 17:02
  • Again, you are absolutely correct. However, the OP actually included the format that he wanted to use in the question i.e. create using new. Correspondingly I answered in that format. – oldcoder Jun 18 '17 at 06:23
0

The DateTimePicker.Value property is already a DateTime. Just reference it:

DateTime d = dateTimePicker1.Value;

If you're only interested in the date and wish to ignore the time, use the DateTime.Date property:

DateTime d = dateTimePicker1.Value.Date;
Grant Winney
  • 65,241
  • 13
  • 115
  • 165