[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Date Format Issues in ASP.Net

Posted on 2007-08-06
4
Medium Priority
?
1,502 Views
Last Modified: 2013-11-26
Hi .
I have a few issues regarding the Date Formatting in ASP.Net.
The User will enter the dates in 'dd-MM-yyyy' format

I have written the following procedure to return the formatted date according to the Parameter specified
====================

    Public Shared Function ReturnFormattedDate(ByVal Date_String As String, ByVal Date_Type As Integer) As String
            Dim GB As New CultureInfo("en-GB")
        Dim US As New CultureInfo("en-US", False)
        Dim ClcDate As Date
     
        If Date_Type = 0 Then
                     ClcDate = CDate(Date_String)
           ReturnFormattedDate = ClcDate.ToString("d", US.DateTimeFormat) ''MM-dd-yyyy

        ElseIf (Date_Type = 1) Then
            ClcDate = CDate(Date_String)
            ReturnFormattedDate = ClcDate.ToString("G", US) ''MM-dd-yyyy hh:mm:ss tt
        ElseIf Date_Type = 2 Then
            'ClcDate = CDate(Date_String)
            ReturnFormattedDate = ClcDate.ToString("d", GB) ''dd-MM-yyyy
        ElseIf Date_Type = 3 Then
            ClcDate = CDate(Date_String)
            ReturnFormattedDate = ClcDate.ToString("G", GB) ''dd-MM-yyyy hh:mm:ss tt
        ElseIf Date_Type = 4 Then
            ClcDate = CDate(Date_String)
            ReturnFormattedDate = ClcDate.ToString("T", GB) ''hh:mm:ss tt
        End If
     End Function

===============
But the  Above procedure does give error if the date is > 12
If User enters date as 28-07-2007, sometimes i want that date to be formatted to '07-28-2007' which is not happening here. Plz have a look and suggest me.
Thanx in Advance
Jenvin


0
Comment
Question by:jenvin
  • 2
4 Comments
 
LVL 16

Expert Comment

by:TSmooth
ID: 19637731
Try using the Date.TryParseExact() static method. This will allow you to make sure that the date is being input in the format you expect. I believe your code is by default assuming MM-dd-yyyy when you use CDate().

To first get your date_string into a proper date object try the following:
If Date.TryParseExact(Date_String, "dd-MM-yyyy", Nothing, System.Globalization.DateTimeStyles.None, ClcDate) = False Then
  Response.Write("Date was not of the correct format") ' Do your error handling here.
End If

This will properly convert the date_string to a date object based on the specified format. You can also use an array of acceptable formats as the second parameter to the TryParseExact method and you can also look into the other parameters on MSDN to see if you need to use any of them.
0
 
LVL 9

Accepted Solution

by:
william007 earned 750 total points
ID: 19637747
This is because when doing Cdate, it assume you to pass in MM-dd-yyyy. Hence, to play safe, it is better to explicitly doing this conversion procedure. Followed is the edited code for ReturnFormattedDate:

    Public Shared Function ReturnFormattedDate(ByVal Date_String As String, ByVal Date_Type As Integer) As String
        Dim GB As New CultureInfo("en-GB")
        Dim US As New CultureInfo("en-US", False)
        Dim str() As String = Date_String.Split("-")
        'Assume the User will enter the dates in 'dd-MM-yyyy' format
        Dim ClcDate As Date = New Date(str(2), str(1), str(0))

        If Date_Type = 0 Then
            ReturnFormattedDate = ClcDate.ToString("d", US.DateTimeFormat) ''MM-dd-yyyy
        ElseIf (Date_Type = 1) Then
            ReturnFormattedDate = ClcDate.ToString("G", US) ''MM-dd-yyyy hh:mm:ss tt
        ElseIf Date_Type = 2 Then
            ReturnFormattedDate = ClcDate.ToString("d", GB) ''dd-MM-yyyy
        ElseIf Date_Type = 3 Then
            ReturnFormattedDate = ClcDate.ToString("G", GB) ''dd-MM-yyyy hh:mm:ss tt
        ElseIf Date_Type = 4 Then
            ReturnFormattedDate = ClcDate.ToString("T", GB) ''hh:mm:ss tt
        End If
    End Function
0
 

Author Comment

by:jenvin
ID: 19661261
Thankx William007.
It is working that way.
Thanks to Tsmooth too

jenvin
0
 
LVL 9

Expert Comment

by:william007
ID: 20073917
Hi jenvin, if your problem has been solved, you should consider accepting the answer(s) that helped you:)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month20 days, 3 hours left to enroll

872 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