Solved

Date format

Posted on 2004-08-17
2
220 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
Comment Utility
Have you tried the DateTime.Parse (or ParseExact) - methods?

Hope this helps,

Joris
0
 
LVL 4

Accepted Solution

by:
Daniellus83 earned 100 total points
Comment Utility

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now