Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Conversion 1

Status
Not open for further replies.

kennedymr2

Programmer
May 23, 2001
594
AU
I seem to be stuck with a data conversion..!@!!
I possibly have had a brain fade..

1. If a date is eg 31st December 2010 how do i get it to 31/12/2010

2. How do i convert todays date eg date() now() to eg 31st December 2010

Really appreciate some help

Regards Kennedymr2
 

in code
dim DatDate as Date
dim StrDate as string

DatDate= Now
StrDate = ShowDateInEnglishFormat(DatDate)
DEBUG.Print StrDate

function ShowDateInEnglishFormat(ThisDate as Date) as string
ShowDateInEnglishFormat= Format (ThisDate, "dd/mm/yyyy")
END FUNCTION
 
But be aware that input such as "31st December 2010" (i.e. part 1 of your question) won't be recognised by VB as a date and therefore will not be (re)formatted correctly
 
Very good point. You'd want to preparse and clean that format up before feeding it into CDate(). The ordinal suffix (31st vs. 31) is a trouble spot going in either direction. You might as well insist on roman numerals.
 
Thanks for the comments.........

I though there would be some sort of vb6 function that converted this date format... i was mistaken...

I have resolved by removing hte st nd etc bit

Dim pos As Long
pos = 2

If IsNumeric(Mid(cd2, 2, 1)) Then pos = 3
If pos = 2 Then
cd2 = Mid(cd2, 1, 1) & Mid(cd2, 4, 99)
End If
If pos = 3 Then
cd2 = Mid(cd2, 1, 2) & Mid(cd2, 5, 99)
End If

Really appreciate the help that is allways forthcoming on tek_tips.

Regards Kennedymr2
 
Just for fun, here's an alternative method of stripping out the unwanted suffixes:
Code:
[blue]With CreateObject("vbscript.regexp")
    .Pattern = "st|nd|rd|th"
    cd2 = .Replace(cd2, "")
 End With[/blue]

 
strongm.....

Thanks for the tip.

A much more '''attractive'' way of carrying out the procedure..

I have altered my program to this method.

Have never had a lot to do with using scripts...will now have a look at the whole script area..

Many thanks Kennedymr2
 
>never had a lot to do with using scripts

This is more a demonstration of Regular Expressions than of scripting
 
Use this method with care. It might ruin some simple dates like "10 August 2011". The Replace method will strip 'st' from August leaving "10 Augu 2011".
 
Given that this example was specifically and explicitly designed to deal with suffixes (i.e. NOT examples without suffixes) this would not be a problem.

However if we want be genereic there is only a minor change, something like the following:

Code:
[blue]With CreateObject("vbscript.regexp")
    .Pattern = "(\d)(st|nd|rd|th)"
    cd2 = .Replace(cd2, "$1")
End With[/blue]
 
Even with early binding and creating the object once, Clean1 takes about 6 times as long as Clean2 here:
Code:
Private RegExp As VBScript_RegExp_55.RegExp

Private Function Clean1(ByVal DateString As String) As String
    With RegExp
        .Pattern = "(\d)(st|nd|rd|th)"
        Clean1 = .Replace(DateString, "$1")
    End With
End Function

Private Function Clean2(ByVal DateString As String) As String
    Dim Junk As Integer
    
    Junk = IIf(Mid$(DateString, 2, 1) Like "#", 3, 2)
    Clean2 = Left$(DateString, Junk - 1) & Mid$(DateString, Junk + 2)
End Function
Not that performance is an issue here, but a simple straightforward approach can work as well or better.
 
I'm not sure that I agree that Regular Expressions are not straightforward. Unfamiliar to some, I agree. But pretty straightforward.

(And Clean1 does have the minor advantage that it also works against alternative date formats such as "Jan 1st 2011" ;-0 )

As ever, however, good to be able to provide alternatives.

 
I though there would be some sort of vb6 function that converted this date format... i was mistaken...

There is.
FormatDateTime()


2. How do i convert todays date eg date() now() to eg 31st December 2010
FormatDateTime(now(),vbLongDate)





Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
I'm pretty sure it doesn't do ordinal suffixes, which I missed the first time. And it does nothing about going the other way. And vbLongDate doesn't do it, and that will produce different results based on the user's settings.

And...

You get the idea.
 
Ok so you want to CAST a date as a string type to a variable of date type.


Use the CDate function in that case.



Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Converting a value from one type from another is not "casting" it at all. "Casting" is when you tell the compiler "even though I have typed this variable's underlying memory as X please treat it as Y and let the chips fall where they may."

I'm not sure why this is such a seldom understood point.
 
True, but type "conversion" is the nearest that Visual Basic ever gets to "casting".

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
>True, but type "conversion" is the nearest that Visual Basic ever gets to "casting".

No, it isn't. VB happily does casting.

 
Vb.Net when using "Option Strict On" sure.


Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top