[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

Date format

Hi,
i'm new to vb .net, i have some problem of formatting date time. i take user input of month and year and need to convert it into a date range, let say, 7,2004 need to be converted to 7/1/2004 to 7/31/2004. How do i convert the 7 and 2004 into a date type? can i do deduction of date?like if i deduct 3/1/2004 it wil gives me 2/29/2004??
0
sinwee80
Asked:
sinwee80
1 Solution
 
jzwaenepoelCommented:
Have you tried the DateTime.Parse (or ParseExact) - methods?

Hope this helps,

Joris
0
 
Daniellus83Commented:

Joris is right, but this is not the full code,.. since you need a try-catch-statement to filter formats like these: "1/12004" in stead of "1/1/2004".

>>>> you need this code (I used a textbox, a button and a label):


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Label1.Text = DateTime.Parse(TextBox1.Text).ToString
        Catch ex As Exception
            MsgBox("Date not recognised")
        End Try
    End Sub



>>>> The Deduct-question...  Use the same setting as before (with textbox, button and label) but now with a EXTRA textbox. Put this code to your app:


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim yourdate As DateTime
        Try
            yourdate = DateTime.Parse(TextBox1.Text)
            yourdate = yourdate.AddDays(-1 * Val(TextBox2.Text))
            Label1.Text = yourdate
        Catch ex As Exception
            MsgBox("Date not recognised")
        End Try
    End Sub


I this case your date will be deducted by the #number of days you fill into the textbox2 !!!


Try it and maybe you like it!

Daniƫl
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now