473,395 Members | 1,584 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Convert RFC Dates to VBA Date

Oralloy
988 Expert 512MB
Folks,

I'm using VBA to access an XML data source that's giving me RFC style dates. Is there a simple way to convert the dates to VBA's internal Date format?

For example, I extract this date string from my XML: Tue, 23 Nov 2010 09:42:07 -0800

What I'd like is a function that returns the VBA Date value #11/23/2010 09:42:07#

Is there something out there, or do I get to write yet another date parser?

Thanks,
Oralloy
May 18 '11 #1

✓ answered by Guido Geurs

Split the string and recombine in the format you like:
Expand|Select|Wrap|Line Numbers
  1. Dim SPLITARRAY() As String
  2. '§ split on spaces
  3.    SPLITARRAY = Split(Text1.Text, " ")
  4.    Select Case SPLITARRAY(2)
  5. '§ convert month to number
  6.    Case "Jan": SPLITARRAY(2) = "01"
  7.    Case "Feb": SPLITARRAY(2) = "02"
  8.    Case "Mar": SPLITARRAY(2) = "03"
  9.    Case "Apr": SPLITARRAY(2) = "04"
  10.    Case "May": SPLITARRAY(2) = "05"
  11.    Case "Jun": SPLITARRAY(2) = "06"
  12.    Case "Jul": SPLITARRAY(2) = "07"
  13.    Case "Aug": SPLITARRAY(2) = "08"
  14.    Case "Sep": SPLITARRAY(2) = "09"
  15.    Case "Okt": SPLITARRAY(2) = "10"
  16.    Case "Nov": SPLITARRAY(2) = "11"
  17.    Case "Dec": SPLITARRAY(2) = "12"
  18.    End Select
  19. '§ combine new string
  20.    Text2.Text = "#" & SPLITARRAY(2) & "/" & SPLITARRAY(1) & "/" & SPLITARRAY(3) & " " & SPLITARRAY(4) & "#"

7 2346
Guido Geurs
767 Expert 512MB
Split the string and recombine in the format you like:
Expand|Select|Wrap|Line Numbers
  1. Dim SPLITARRAY() As String
  2. '§ split on spaces
  3.    SPLITARRAY = Split(Text1.Text, " ")
  4.    Select Case SPLITARRAY(2)
  5. '§ convert month to number
  6.    Case "Jan": SPLITARRAY(2) = "01"
  7.    Case "Feb": SPLITARRAY(2) = "02"
  8.    Case "Mar": SPLITARRAY(2) = "03"
  9.    Case "Apr": SPLITARRAY(2) = "04"
  10.    Case "May": SPLITARRAY(2) = "05"
  11.    Case "Jun": SPLITARRAY(2) = "06"
  12.    Case "Jul": SPLITARRAY(2) = "07"
  13.    Case "Aug": SPLITARRAY(2) = "08"
  14.    Case "Sep": SPLITARRAY(2) = "09"
  15.    Case "Okt": SPLITARRAY(2) = "10"
  16.    Case "Nov": SPLITARRAY(2) = "11"
  17.    Case "Dec": SPLITARRAY(2) = "12"
  18.    End Select
  19. '§ combine new string
  20.    Text2.Text = "#" & SPLITARRAY(2) & "/" & SPLITARRAY(1) & "/" & SPLITARRAY(3) & " " & SPLITARRAY(4) & "#"
May 18 '11 #2
Oralloy
988 Expert 512MB
Guido,

Your singleton solution may well be what I need to do. Thank you.

I was hoping that there was a common library routine that I'd failed to recognize, which actually handled most of the major date formats. I've written such beasts before, but they take time to implement and debug properly.

Cheers!
Oralloy
May 18 '11 #3
Couldn't you just do something like this,

Expand|Select|Wrap|Line Numbers
  1. Sub test()
  2. Dim mystring As String
  3. Dim myDate As Date
  4.  
  5. mystring = "Tue, 23 Nov 2010 09:42:07 -0800"
  6. myDate = Mid(mystring, 5, 20)
  7. mystring = "#" & Left(CStr(myDate), 18) & "#"
  8.  
  9. End Sub
  10.  
That should get you the string you need.
May 20 '11 #4
Guido Geurs
767 Expert 512MB
Sorry, not quite the same.

He wants: #11/23/2010 09:42:07#

Your code ("#" & Left(CStr(myDate), 18) & "#") gives:
#23/11/2010 9:42:00#
- day and month not on the same place.
- hours is not "09"
- second = 00 ? => it must be: Mid(mystring, 6, 20)
- why Left(...?? => you can just write:
("#" & CStr(myDate) & "#") because Mydate= 18 long.

But I think this is better:
Expand|Select|Wrap|Line Numbers
  1. "#" & CStr(Format(myDate, "mm/dd/yyyy hh:mm:ss")) & "#"
giving:
#11/23/2010 09:42:07#
May 21 '11 #5
Oralloy
988 Expert 512MB
Yep folks,

Good answers. Thank you.

Actually, the CDate(Mid$(...)) solution is the quick answer I settled on earlier, right after Guido tossed out his quick date parser.

My next question would be if we have a general date handling function anywhere in the community? Part of my problem being that people can enter:
  • Tue, 23 Nov 2010 09:42:07 -0800
  • 11/23/2010
  • 23 Nov 2010
  • 2010-11-23 15:23

I did such a beast for a commercial web-site using perl a number of years ago; and it worked great. I guess I'm just being lazy, though.
May 23 '11 #6
Guido Geurs
767 Expert 512MB
A question: is it possible to direct the user to enter a date in a well defined format like you find in some web pages: with drop down lists to select the values of day, month and year and the result is a date in your format in a non editable textbox?

Otherwise: you can recognize the formats on distinguished differences like:
- the 1st has a comma
- the 2nd has slashes
- the 3rd has only spaces
- the 4th has "-" signs
so you can write a code like:
Expand|Select|Wrap|Line Numbers
  1. If instr(datestring,",") then
  2.    myDate = Mid(mystring, 5, 20)
  3.    mystring="#" & CStr(Format(myDate, "mm/dd/yyyy hh:mm:ss")) & "#" 
  4. else if instr(datestring,"/") then
  5. ...
  6. else if instr(datestring,"-") then
  7. ...
  8. else
  9. ...
  10. end if
May 23 '11 #7
Oralloy
988 Expert 512MB
Not really.

My problem is that I have built a Word report that extracts its data from a vendor product, and the admin may change the date format from underneath me. I'd like the report to continue working correctly in said case, rather than have my report fail.

Yes, I do expect that my customer will be changing date format. Call it "man's intuition". It's a new system, and they just haven't figured out that they want dates to read a little "better".

As I said, I'm being lazy and hoping the function exists, because the approach you're suggesting is exactly the correct one.

The bottom line is that it doesn't, so I'll deal with it.

Thank you.
May 23 '11 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
1
by: ABC | last post by:
How to convert a date string to datetime value with custom date format? e.g. Date String Date Format Result (DateTime value) "05/07/2004" "MM/dd/yyyy" May 7, 2004 "01062005" ...
1
by: h_ghanaty | last post by:
how i can convert gregorian date to jalaly(Iran date) with vb.net
1
by: Raja | last post by:
Hi Anyone has any idea of how to convert Julian Date (eg. 12744057862..) to a VB.NET date time. Thanks in advance.
1
by: Sam | last post by:
How do I convert Julian Date to Calendar Date in ASP.Net 1.1 based on following guideline found at Internet? To convert Julian date to Gregorian date: double JD = 2299160.5; double Z =...
4
by: Daniel Kaseman | last post by:
How do I convert a date into a serial number? (I'm trying to enter a FROM date and a TO date, then make my PROGRESS BAR show how close I am to the TO date.) get it? I know that MS Excel...
2
by: Kakishev | last post by:
I have a problem at how best to convert a Date into a text field and keep the format dd/mmm/yyyy (01-FEB-2007). The problem is that dates are imported from SQL into an access front end Database....
3
by: lyne_asp | last post by:
Please help me to convert excel date to asp date. Here is my code objConnEx.open "Provider=Microsoft.Jet.OLEDB.4.0;Data...
1
by: magicscreen | last post by:
I am trying to create a CSV export file that has a date field. If you look at the table in Access the date looks like mm/dd/yyyy. I want to run the export wizard to create a spec so I can use it...
1
by: smdmca | last post by:
I have Julian date, I want to convert it into date. Is there any function in MySql to convert Julian date to date eg- Julian Date- 2455116 Date - Oct - 12, 2009
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.