Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-04-23
9
Medium Priority
?
312 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Loops Section Overview

876 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