Solved

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

Posted on 2014-04-23
9
305 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

808 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