如何在datetimepicker中设置默认月份和日期?

时间:2017-08-15 04:01:34

标签: c# winforms

我在网上搜索过,我只设法找到设置年月日的代码。

dateTimePicker2.CustomFormat = "DD/MM";
dateTimePicker2.Value = new DateTime(12,31);

我尝试使用自定义格式,似乎无法正常工作

$fname = $_GET['fname'];
$lname = $_GET['lname'];
$mname = $_GET['mname'];
$dob = $_GET['dob'];
$pnumber = $_GET['pnumber'];
$occupation = $_GET['occupation'];
$joindate = $_GET['join date'];
$ffname = $_GET['ffname'];
$flname = $_GET['flname'];
$peraddress = $_GET['peraddress'];
$fpnumber = $_GET['fpnumber'];
$mpnumber = $_GET['mpnumber'];
$roomnumber = $_GET['roomnumber'];
$deposite = $_GET['deposite'];
//$password = $_GET['password'];
//$sha1password = sha1($password);

//create connection
$connection = mysqli_connect('localhost', 'root', 'root', 'Hostel');

//check connection
if ($connection->connect_error) {
die('Connection error (' . $connection->connect_errno . ') ' .$connection->connect_error);
}

echo 'Cool!' .$connection->host_info . "\n";

 $sqlin = "INSERT INTO Student (First_Name, Last_Name, Middle_Name,   Date_of_Birth, Phone_Number, Occupation, Join_Date, Father_First_Name, Father_Last_Name, Permenent_Address, Father_Phone_Number, Mother_Phone_Number, Room_Number, Deposite)
 VALUES ('$fname', '$lname', '$mname', '$dob', '$pnumber', '$occupation', '$joindate', '$ffname', '$flname', '$peraddress', '$fpnumber', '$mpnumber', '$roomnumber', '$deposite')";

 if ($connection->query($sqlin) === TRUE) {
 echo"Thank you! Your info has been entered into database";
 }else{
    echo"Error: " . $sqlin . "<br>" . $connection->error;
 }

 $connection->close();

 ?>

3 个答案:

答案 0 :(得分:0)

您不能在没有一年的情况下部分设置日期,它无效。

你可以做的是在代码中指定月份和日期为&#34;默认&#34;值,并以程序方式(或任何你想要的年份)获得当前年份,并使用该年份的值。

答案 1 :(得分:0)

您无法仅从日期和月份创建DateTime对象。 DateTime根本没有这种构造函数。 DateTime Constructors

所以你需要采取某种&#34;解决方法&#34;
  - 使用&#34; dummy&#34;年份以及何时需要使用日期 - 仅使用MonthDay属性。

var dummyYear = 2000;
dateTimePicker2.Value = new DateTime(dummyYear, 12, 31);

另一种解决方法是使用ParseExact方法,根据您使用的格式创建DateTime&#34; dd / MM&#34;

var date = DateTime.ParseExact("31/12", "dd/MM", CultureInfo.InvariantCulture); 
dateTimePicker2.Value = date; // 12/31/2017

请注意,当您未提供一年时 - 将使用当前年份 另一个通知:DD是无效格式的天数应该是小写&#34; dd&#34;

答案 2 :(得分:0)

dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "dd/MM";
dateTimePicker1.Value = DateTime.Now;