Solved

Reformating a Date within a String

Posted on 2016-09-09
12
50 Views
Last Modified: 2016-09-09
I have a very unique request to reformat a date that is within a string.

<RequestedDate Type="DeliverOn">9/12/2016</RequestedDate>

Needs to read: <RequestedDate Type="DeliverOn">2016-09--12</RequestedDate>

It is a dynamic date..  My thought was something like grabbing the data between >< to work with it.
- data between > and first /  = month and has to be in a "MM" fomat with leading 0 < 10
- data between first / and second /  = day and needs to be "DD" format with leading 0 < 10
- data between second / and <  = year in "YYYY" format

last concatenate   year&"-"&month&"-"&day      Resulting in a format of  YYYY-MM-DD

Is this possible to grab the specific values from the string and name them so they can be used in a concatenation/reformat of the string?

It is not possible to reformat prior to string, because when the .xml is generated it chg's the format on me.  Any idea's?
0
Comment
Question by:RWayneH
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +2
12 Comments
 
LVL 20

Expert Comment

by:Roy_Cox
ID: 41791477
Can you provide an example in a workbook. You would probably need to use a combination of formulas - Mid, Left, Right,.
0
 

Author Comment

by:RWayneH
ID: 41791505
Not really, it is a very busy, very large book with lots of proprietary data.  I wanted to make it simple by providing the string.  All I am going to do is make active a cell on a sheet and have the reformat run.  Sheet tab name is SIF Data and the cell is A22, that part is going to be static, name the cell to get the string and run a reformat.  So if you could work with the provided string as it is produced by an external process, that would be great.  It will take all the questions about how the string was generated etc. out of the task at hand.  Can this be done as explained?
0
 
LVL 20

Expert Comment

by:Roy_Cox
ID: 41791511
It's not clear from a String whether the date the 9th December or the 12th of September.

Will you have this string in say A1 and a formula can re-format in B1?
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 21

Assisted Solution

by:crystal (strive4peace) - Microsoft MVP, Access
crystal (strive4peace) - Microsoft MVP, Access earned 83 total points
ID: 41791539
Function FormatDateFromXMLString(pString As String) As String
'160909 strive4peace
'returns a STRING not a date
'pString example: <RequestedDate Type="DeliverOn">9/12/2016</RequestedDate>
'return: "2016-09-12"
   On Error GoTo Proc_Err
   Dim iPos As Integer _
      , iPos2 As Integer _
      , sDate As String _
      , dDate As Date
   'initialize return value
   FormatDateFromXMLString = ""
   'look for >
   iPos = InStr(pString, ">")
   If iPos > 0 Then
      'look for <
      iPos2 = InStr(iPos + 1, pString, "<")
      If iPos2 > 0 Then
         'get what is between them
         sDate = Mid(pString, iPos + 1, iPos2 - iPos - 1)
         'convert to a date
         dDate = CDate(sDate)
         'format the date
         FormatDateFromXMLString = Format(dDate, "yyyy-mm-dd")
      End If
   End If
Proc_Exit:
   On Error Resume Next
   Exit Function
Proc_Err:
   Resume Proc_Exit
End Function

Open in new window

0
 
LVL 43

Assisted Solution

by:Saqib Husain, Syed
Saqib Husain, Syed earned 83 total points
ID: 41791619
Monster of a formula

=REPLACE(A1,FIND(">",A1,FIND(">",A1,FIND("RequestedDate",A1)))+1,9,RIGHT(SUBSTITUTE(REPLACE(REPLACE(A1,1,FIND(">",A1,FIND(">",A1,FIND("RequestedDate",A1))),""),FIND("<",REPLACE(A1,1,FIND(">",A1,FIND(">",A1,FIND("RequestedDate",A1))),"")),9999,""),"/","-"),4)&"-"&LEFT(SUBSTITUTE(REPLACE(REPLACE(A1,1,FIND(">",A1,FIND(">",A1,FIND("RequestedDate",A1))),""),FIND("<",REPLACE(A1,1,FIND(">",A1,FIND(">",A1,FIND("RequestedDate",A1))),"")),9999,""),"/","-"),LEN(SUBSTITUTE(REPLACE(REPLACE(A1,1,FIND(">",A1,FIND(">",A1,FIND("RequestedDate",A1))),""),FIND("<",REPLACE(A1,1,FIND(">",A1,FIND(">",A1,FIND("RequestedDate",A1))),"")),9999,""),"/","-"))-5))
0
 
LVL 22

Assisted Solution

by:Ejgil Hedegaard
Ejgil Hedegaard earned 83 total points
ID: 41791646
The formula could be a bit shorter
=LEFT(A1,FIND(">",A1))&TEXT(DATEVALUE(A2),"yyyy-mm-dd")&RIGHT(A1,LEN(A1)-FIND("<",A1,2)+1)

Open in new window

Or if the system settings does not recognize US date format (like mine) this
=LEFT(A1,FIND(">",A1))&TEXT(DATE(MID(A1,FIND("<",A1,2)-4,4),MID(A1,FIND(">",A1)+1,FIND("/",A1)-FIND(">",A1)-1),MID(A1,FIND("/",A1)+1,FIND("/",A1,FIND("/",A1)+1)-FIND("/",A1)-1)),"yyyy-mm--dd")&RIGHT(A1,LEN(A1)-FIND("<",A1,2)+1)

Open in new window

0
 
LVL 20

Accepted Solution

by:
Roy_Cox earned 251 total points
ID: 41791670
Reading the question, I think the OP wants to format the existing text. This would require VBA. The Function provide by Crystal would work like this

Option Explicit

Sub ReFormat()
Dim str As String

str = ActiveCell.Value
ActiveCell.Value = Left(str, 32) & FormatDateFromXMLString(str) & Right(str, 16)
End Sub
Function FormatDateFromXMLString(pString As String) As String
'160909 strive4peace
'returns a STRING not a date
   Dim iPos As Integer _
      , iPos2 As Integer _
      , sDate As String _
      , dDate As Date
   'initialize return value
   FormatDateFromXMLString = ""
   'look for >
   iPos = InStr(pString, ">")
   If iPos > 0 Then
      'look for <
      iPos2 = InStr(iPos + 1, pString, "<")
      If iPos2 > 0 Then
         'get what is between them
         sDate = Mid(pString, iPos + 1, iPos2 - iPos - 1)
         'convert to a date
         dDate = CDate(sDate)
         'format the date
         FormatDateFromXMLString = Format(dDate, "yyyy-mm-dd")
      End If
   End If
End Function

Open in new window

0
 
LVL 21
ID: 41791731
thanks, Roy -- although I believe a correction from "ActiveCell.Value = Left(str, 32) & FormatDateFromXMLString(str) & Right(str, 16)" to:
ActiveCell.Value =FormatDateFromXMLString(ActiveCell.Value) 

Open in new window

provided that it is desired to convert whatever was in the activecell XML to the date ... however the previous and susequent lines could also contain XML so perhaps it would be better to put formula somewhere else and send a cell reference.  I am busy right now so perhaps you can help with that too? thanks
0
 

Author Comment

by:RWayneH
ID: 41791750
Thank you Roy and Crystal.  It did work.  I do not understand how to Call to a function as Crystal wrote, but Roy your version (based on hers) is working nicely.  Thanks all for the input.
0
 

Author Closing Comment

by:RWayneH
ID: 41791757
All good comments and suggestions.  Appreciate the help.  Thanks.
0
 
LVL 21
ID: 41791790
you're welcome ~ happy to help
0
 
LVL 20

Expert Comment

by:Roy_Cox
ID: 41792264
Pleased to help
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

726 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