Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-04-23
9
Medium Priority
?
309 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 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

722 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