Solved

Date format

Posted on 2004-08-17
2
224 Views
Last Modified: 2012-05-05
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
Comment
Question by:sinwee80
2 Comments
 
LVL 1

Expert Comment

by:jzwaenepoel
ID: 11818920
Have you tried the DateTime.Parse (or ParseExact) - methods?

Hope this helps,

Joris
0
 
LVL 4

Accepted Solution

by:
Daniellus83 earned 100 total points
ID: 11819122

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question