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
Split the string and recombine in the format you like: - Dim SPLITARRAY() As String
-
'§ split on spaces
-
SPLITARRAY = Split(Text1.Text, " ")
-
Select Case SPLITARRAY(2)
-
'§ convert month to number
-
Case "Jan": SPLITARRAY(2) = "01"
-
Case "Feb": SPLITARRAY(2) = "02"
-
Case "Mar": SPLITARRAY(2) = "03"
-
Case "Apr": SPLITARRAY(2) = "04"
-
Case "May": SPLITARRAY(2) = "05"
-
Case "Jun": SPLITARRAY(2) = "06"
-
Case "Jul": SPLITARRAY(2) = "07"
-
Case "Aug": SPLITARRAY(2) = "08"
-
Case "Sep": SPLITARRAY(2) = "09"
-
Case "Okt": SPLITARRAY(2) = "10"
-
Case "Nov": SPLITARRAY(2) = "11"
-
Case "Dec": SPLITARRAY(2) = "12"
-
End Select
-
'§ combine new string
-
Text2.Text = "#" & SPLITARRAY(2) & "/" & SPLITARRAY(1) & "/" & SPLITARRAY(3) & " " & SPLITARRAY(4) & "#"
7 2346
Split the string and recombine in the format you like: - Dim SPLITARRAY() As String
-
'§ split on spaces
-
SPLITARRAY = Split(Text1.Text, " ")
-
Select Case SPLITARRAY(2)
-
'§ convert month to number
-
Case "Jan": SPLITARRAY(2) = "01"
-
Case "Feb": SPLITARRAY(2) = "02"
-
Case "Mar": SPLITARRAY(2) = "03"
-
Case "Apr": SPLITARRAY(2) = "04"
-
Case "May": SPLITARRAY(2) = "05"
-
Case "Jun": SPLITARRAY(2) = "06"
-
Case "Jul": SPLITARRAY(2) = "07"
-
Case "Aug": SPLITARRAY(2) = "08"
-
Case "Sep": SPLITARRAY(2) = "09"
-
Case "Okt": SPLITARRAY(2) = "10"
-
Case "Nov": SPLITARRAY(2) = "11"
-
Case "Dec": SPLITARRAY(2) = "12"
-
End Select
-
'§ combine new string
-
Text2.Text = "#" & SPLITARRAY(2) & "/" & SPLITARRAY(1) & "/" & SPLITARRAY(3) & " " & SPLITARRAY(4) & "#"
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
Couldn't you just do something like this, -
Sub test()
-
Dim mystring As String
-
Dim myDate As Date
-
-
mystring = "Tue, 23 Nov 2010 09:42:07 -0800"
-
myDate = Mid(mystring, 5, 20)
-
mystring = "#" & Left(CStr(myDate), 18) & "#"
-
-
End Sub
-
That should get you the string you need.
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: - "#" & CStr(Format(myDate, "mm/dd/yyyy hh:mm:ss")) & "#"
giving:
#11/23/2010 09:42:07#
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.
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: - If instr(datestring,",") then
-
myDate = Mid(mystring, 5, 20)
-
mystring="#" & CStr(Format(myDate, "mm/dd/yyyy hh:mm:ss")) & "#"
-
else if instr(datestring,"/") then
-
...
-
else if instr(datestring,"-") then
-
...
-
else
-
...
-
end if
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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" ...
|
by: h_ghanaty |
last post by:
how i can convert gregorian date to jalaly(Iran date)
with vb.net
|
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.
|
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 =...
|
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...
|
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....
|
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...
|
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...
|
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
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
| |