DateValue(Now)-7

Hi All
I'm passing today's date on DateTimePicker through Form load as shown below:
Question on DateTimePicker1.VAlue , I want to show the default date like DateValue(Now)-7 , seven days before today's date
Private Sub Form1_Load(ByVal sender As object, ByVal e As EventArgs)
     DateTimePicker1.Value = DateValue(Now)
      DateTimePicker2.Value = DateValue(Now)
      
    End Sub

Thanks for your help
Kind regards,
Farhan

Comments

  • edited 8:25AM
    DateTimePicker1.Value = Date.Now.AddDays(-7)
  • edited October 2017
    ipong wrote: »
    DateTimePicker1.Value = Date.Now.AddDays(-7)


    Thanks Its working but during run its generate following error;

    Unhandles exception has occured in a component in your application
    If you click Continue, the application will ignore this error and attempt to continue
    Conversion from sting "24/09/2017 23:11:05 00:00" to type Date is not valid

    at the bottom of my code I'm using following codes passing the DAtePicker value into variable and concatenate with 00:00
          StartDate = DateTimePicker1.Value
          StartDate = FormatDateTime (StartDate & " 00:00")
          Report.SetParameterValue("StartDate", StartDate)
    

    Thanks for your help
  • edited October 2017
    DateTimePicker returns datetime type, use datetime library, sorry in c#, you may convert with http://converter.telerik.com/:

    a. start date (reset time value to zero, just get the date portion)
    DateTime.Now returns 24/09/2017 23:11:05:001
    DateTime.Now.Date returns 24/09/2017 00:00:00:000

    b. end date (time = 23:59:59:999)
    DateTime.Now.Date.AddDays(1).AddMilliseconds(-1);

    c. convert to string
    DateTime.Now.Date.AddDays(1).AddMilliseconds(-1).ToString("dd /MM/yyyy HH:mm:ss");

    d. beware passing datetime value to ms access database
    ms access doesnt recoqnize milliseconds, therefore, you must use string in this context in order to remove milliseconds
    ToString("dd /MM/yyyy HH:mm:ss");

    e. parameter in fastreport by default is string, but you may change the datatype to int or datetime, etc

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.