Solved

How to Normalise the date? (Converting user-defined dates into meaningful date)

Posted on 2004-10-12
9
259 Views
Last Modified: 2013-12-26
How do I convert (normalise) the user-defined dates into meaningful dates?  
For example:  If the user enters date '29-Feb-2002' must return '1-Mar-2002' (29th Feb is not a valid date and hence the next day is returned).  

Similarly, if the user enters '31-30-2000' must return '1-Jul-2003' (As the months 30 is invalid, it must return 30 months from the year 2000 which results June, 2003 but the date is 31st and there is no 31st in June, it must return the next day i.e. 1-Jul-2003).

Also, I must be able to set my own default date for date calculation.  For example: If I set Default Date as '01-01-2004' and I pass a date i.e. '10-10-2004', it must return the No. of days from 1st Jan, 2004 which is 284 days and if the user enters 284 days, it must return '10-10-2004'.

The User can enter either '01/01/2004' or '01-Jan-2004' or '01-January-2004' formats.  How do I validate the date string?

Thank you Experts in advance.
0
Comment
Question by:kvnagaraj2000
  • 5
  • 2
9 Comments
 
LVL 5

Assisted Solution

by:gary_j
gary_j earned 250 total points
ID: 12286847
[Also, I must be able to set my own default date for date calculation.  For example: If I set Default Date as '01-01-2004' and I pass a date i.e. '10-10-2004', it must return the No. of days from 1st Jan, 2004 which is 284 days and if the user enters 284 days, it must return '10-10-2004']

numDays = dateDiff("d",date1,date2)

newdate = dateadd("d",numDays,date)

As to the rest of your problem, I think you're going to have to write a comprehensive parsing routine or have some rules -- one or the other.  If it is "freestyle" entry, things like the cdate function just are not reliable.
0
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12287359
To validate the dates use IsDate - but beware of it as it does this: First it attempts to validate the date in the user's locale, if it fails it will swap the day and month and try again. I have a better conversion routine which I use in my calendar control. I will try and dig it out.

0
 
LVL 5

Expert Comment

by:gary_j
ID: 12287415
the problem with isdate is that it will completely fail on things like ('31-30-2000').  
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 10

Accepted Solution

by:
fds_fatboy earned 250 total points
ID: 12287637
Here it is I have attempted to get the relevant bits: - in fact, I will comment out the bits that are not relevant for you. If you reinstate the commented out parts, all invalid dates will return error 5, otherwise it will let 29 feb 2001 etc through returning 1 Mar 2001 etc.

Private MstrDateSeparator As String 'Elsewhere set to "/"
Private Const McstrValidSeparators As String = "/. ,~_-"

Public Enum DateEntryOrder
'    [Default VB date] = 0 ' Your locale setting
    [Day/Month/Year]
    [Month/Day/Year]
    [Year/Month/Day]
End Enum

Private Function Text2Date(PstrText As String, Optional PlngEntryOrder As DateEntryOrder = [Default VB date]) As Date
    Dim LlngIndex As Long
    Dim LstrDatepart() As String
    Dim LdteDate As Date

    On Error GoTo ErrorHandler

    For LlngIndex = 1 To Len(PstrText)
        If Mid$(PstrText, LlngIndex, 1) Like "[" & McstrValidSeparators & "]" Then
            Mid$(PstrText, LlngIndex, 1) = MstrDateSeparator
        End If
    Next

    LstrDatepart = Split(PstrText, MstrDateSeparator)

    Select Case PlngEntryOrder
'    Case DateEntryOrder.[Default VB date]
'        Text2Date = CDate(PstrText)
    Case DateEntryOrder.[Day/Month/Year]
        If Not IsNumeric(LstrDatepart(1)) Then
            LstrDatepart(1) = DatePart("m", "13 " & LstrDatepart(1) & " 2000")
        End If

        LdteDate = DateSerial(CInt(LstrDatepart(2)), CInt(LstrDatepart(1)), CInt(LstrDatepart(0)))

'        If CInt(DatePart("d", LdteDate)) = CInt(LstrDatepart(0)) And _
'           CInt(DatePart("m", LdteDate)) = CInt(LstrDatepart(1)) And _
'           CInt(DatePart("yyyy", LdteDate)) = CInt(LstrDatepart(2)) Then
           Text2Date = LdteDate
'        Else
'            Err.Raise 5
'        End If

    Case DateEntryOrder.[Month/Day/Year]
        If Not IsNumeric(LstrDatepart(0)) Then
            LstrDatepart(0) = DatePart("m", "13 " & LstrDatepart(0) & " 2000")
        End If

        LdteDate = DateSerial(CInt(LstrDatepart(2)), CInt(LstrDatepart(0)), CInt(LstrDatepart(1)))

 '       If CInt(DatePart("d", LdteDate)) = CInt(LstrDatepart(1)) And _
 '          CInt(DatePart("m", LdteDate)) = CInt(LstrDatepart(0)) And _
 '          CInt(DatePart("yyyy", LdteDate)) = CInt(LstrDatepart(2)) Then
           Text2Date = LdteDate
 '       Else
 '           Err.Raise 5
 '       End If

    Case DateEntryOrder.[Year/Month/Day]
        If Not IsNumeric(LstrDatepart(1)) Then
            LstrDatepart(1) = DatePart("m", "13 " & LstrDatepart(1) & " 2000")
        End If

        LdteDate = DateSerial(CInt(LstrDatepart(0)), CInt(LstrDatepart(1)), CInt(LstrDatepart(2)))

 '       If CInt(DatePart("d", LdteDate)) = CInt(LstrDatepart(2)) And _
 '          CInt(DatePart("m", LdteDate)) = CInt(LstrDatepart(1)) And _
 '          CInt(DatePart("yyyy", LdteDate)) = CInt(LstrDatepart(0)) Then
           Text2Date = LdteDate
 '       Else
 '           Err.Raise 5
 '       End If

    Case Else
        Err.Raise 5
    End Select

    Exit Function
ErrorHandler:
    With Err
         .Raise .Number, .Source, .Description, .HelpFile, .HelpContext
    End With

End Function
0
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12287692
gary_j:

Yes - and IsDate is crap. e.g. A user's locale says dates are in dd/mm/yyyy format and they type in 12/31/2004, Is date says it's ok.
0
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12287821
My earlier example distilled for this problem:

Private Const McstrValidSeparators As String = "/. ,~_-"

Private Function Text2Date(ByVal PstrText As String) As Date
    Dim LlngIndex As Long
    Dim LstrDatepart() As String

    On Error GoTo ErrorHandler

    PstrText = Trim$(PstrText)

    If PstrText = vbNullString then
        Err.Raise 5
    End If

    For LlngIndex = 1 To Len(PstrText)
        If Mid$(PstrText, LlngIndex, 1) Like "[" & McstrValidSeparators & "]" Then
            Mid$(PstrText, LlngIndex, 1) = "/"
        End If
    Next

    LstrDatepart = Split(PstrText, MstrDateSeparator)

    If UBound(LstrDatepart) <> 2 Then
        Err.Raise 5
    End If

    If Not IsNumeric(LstrDatepart(1)) Then
        LstrDatepart(1) = DatePart("m", "13 " & LstrDatepart(1) & " 2000")
    End If

   'in this case entry is d/m/y - LstrDatePart (0) is Day; (1) is Month; (2) is Year  
   Text2Date = DateSerial(CInt(LstrDatepart(2)), CInt(LstrDatepart(1)), CInt(LstrDatepart(0)))
    Exit Function
ErrorHandler:
    With Err
         .Raise .Number, .Source, .Description, .HelpFile, .HelpContext
    End With

End Function
0
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12814469
points split Gary_j/fds_fatboy?
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ms access #TYPE! error on report when no data 4 76
Visual Studio 2005 text editor 10 38
Added a column screws up code 5 57
Send outlook email from VBS Script 2 29
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

770 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