Solved

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

Posted on 2014-04-23
9
299 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:stephenlecomptejr
Comment Utility
I also do have using System.Text.RegularExpressions; at the top as well.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:stephenlecomptejr
Comment Utility
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
Comment Utility
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

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
Comment Utility
I appreciate the replies!
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now