String в DateTime - C#
Формулировка задачи:
Мне выдает ошибку "Строка не распознана как действительное значение DateTime" при запуске вот этого кода
Код
Прога полностью во вложении.
В чем проблема, где ошибка, как решить?
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace WindowsFormsApplication2
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- monthCalendar1.MaxDate=(new DateTime(2256, 12, 31));
- monthCalendar1.MinDate = (new DateTime(1960, 1, 01));
- numericUpDown1.Maximum = 31;
- numericUpDown2.Maximum = 12;
- numericUpDown3.Maximum = 2256;
- numericUpDown1.Minimum = 1;
- numericUpDown2.Minimum = 1;
- numericUpDown3.Minimum = 1960;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- int daycur = (int)numericUpDown1.Value;
- int monthcur = (int)numericUpDown2.Value;
- int yearcur = (int)numericUpDown3.Value;
- string day1 = (Convert.ToString(numericUpDown1));
- string month1 = (Convert.ToString(numericUpDown2));
- string year = (Convert.ToString(numericUpDown3));
- string separator = "/";
- string day = String.Join(day1,separator);
- string month = String.Join(month1, separator);
- string date1 = String.Join(month,day,year);
- DateTime date = DateTime.ParseExact(date1, "MM/dd/yyyy",
- null);
- monthCalendar1.SetDate(date);
- }
- }
- }
Решение задачи: «String в DateTime»
textual
Листинг программы
- private void button1_Click(object sender, EventArgs e)
- {
- int day = (int)numericUpDown1.Value;
- int month = (int)numericUpDown2.Value;
- int year = (int)numericUpDown3.Value;
- DateTime date = new DateTime(year, month, day);
- monthCalendar1.SetDate(date);
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д