Solved

Need C# sharp function that takes a string and parses out date

Posted on 2014-04-23
9
308 Views
Last Modified: 2014-04-24
All,

I need a function that can take the following values and return me the date in a format:  MM-dd-yyyy.

2014-4-22-Georgia Report
2014-12-22-New Orleans Report
2014-3-1-San Fransico Report

I think in every scenario there will be the same number of dashes - so I will always need to look for the 3rd dash and then take everything before that to parse out the date - then format.

I'm working on various code at the moment and I was hoping someone could give me the exact syntax on making this work correctly.  Thank you sincerely in advance!
0
Comment
Question by:stephenlecomptejr
[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
  • 2
  • 2
  • +1
9 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40019226
using System.Text.RegularExpressions;

...

public string GetDate(string source)
{
    Match m = Regex.Match(source, "^[0-9]{4}-[0-9]{2}-[0-9]{2}");
    DateTime d;

    if (m.Success && DateTime.TryParse(m.Value, out d))
    {
        return d.ToString("MM/dd/yyyy");
    }
    else
    {
        return string.Empty;
    }
}

Open in new window

0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 40019232
string value = "2014-11-22-Georgia Report";
int endPosition = value.IndexOf("-");	// Find the position of the last -
endPosition = value.IndexOf('-', endPosition+1);
endPosition = value.IndexOf('-', endPosition + 1);
DateTime dateValue = DateTime.ParseExact(value.Substring(0,endPosition),"yyyy-M-d",null);
string result = dateValue.ToString("MM-dd-yyyy");

Open in new window

0
 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 40019251
I'm getting an error on the line-item:

Match m = Regex.Match(source, "^[0-9]{4}-[0-9]{2}-[0-9]{2}");

The string I'm passing is exactly as: 2014-4-22-SUNY Stony Brook - Datasheets & Catalog Cuts - 1 of 8.pdf
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 40019252
I also do have using System.Text.RegularExpressions; at the top as well.
0
 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 40019290
James as you can see from my post of the string I am using it is not enough to grab the last dash.  Rather I need everything from the third dash left wise
0
 
LVL 6

Expert Comment

by:Brijesh Gandhi
ID: 40019299
If The pattern is same and Date is always at beginning then I suggest to use split function
string source = "2014-4-22-Georgia Report" ;
string[] tmpdate =  source.split('-');
string Date = tempdate[1].Trim() + "-" +  tempdate[2].Trim() + "-" + tempdate[0].Trim();

Open in new window

I hope this might help you..
0
 
LVL 40
ID: 40019345
I am effectively going for the 3rd dash. Once I have it, I convert everything to the left of it. This is exactly what you are looking for

It happen to always be the last in your example, so my way of saying it might not have been right. Even if you had more dashes, say in the city name, my routine would work.
0
 
LVL 1

Author Closing Comment

by:stephenlecomptejr
ID: 40020858
I appreciate the replies!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40020958
I'm getting an error on the line-item:
What is the error?

I do see that the pattern needs to be adjusted slightly:

Match m = Regex.Match(source, "^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}");

Open in new window

0

Featured Post

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

615 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