简体   繁体   中英

Set date picker controls value behind 2 days

Currently the date picker control value by default set to current date. I want to set it to two days before the current date. I am using form controls. I tried the below code,

DatePicker cntrl = new DatePicker();
cntrl.Value = DateTime.Today.AddDays(-2);
this.Controls.Add(cntrl);

<Control>
        <ControlType>DatePicker</ControlType>
        <Left>20</Left>
        <Top>325</Top>
        <Width>150</Width>
        <Name>DatePickerPackDate</Name>
        <DataField>HarvestDate</DataField>
        <MaxDateToday>false</MaxDateToday>
        <DefaultToday>true</DefaultToday>
        <StartWithBlank>false</StartWithBlank>
    </Control>

the above code is not working, it always shows the current date in date picker control.

I want to show 11/16/2015 if today is 11/18/2015. How to forcefully over right the current date to two days back in date picker.

Thanks

You are setting the control to default to today:

<DefaultToday>true</DefaultToday>

Remove this line or set the property to false .

If this is a DatePicker element you need to set the SelectedDate property:

 DatePicker cntrl = new DatePicker();
    cntrl.SelectedDate = DateTime.Today.AddDays(-2);
    this.Controls.Add(cntrl);

If this is a DateTimePicker then you can use the Value property:

 DateTimePicker cntrl = new DateTimePicker();
    cntrl.Value = DateTime.Today.AddDays(-2);
    this.Controls.Add(cntrl);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM