Solved

Create a Method to convert dates

Posted on 2006-07-17
3
205 Views
Last Modified: 2010-04-16
Hi there,

I have multiple fields that I will want to convert to a specific date format but I don't want to have to retype all of the code over and over so I'd like to create a Method that I can pass the field into which would convert it.

Right now I have

string strDate = line.Substring(296,8);
DateTime MyDate = DateTime.ParseExact(strDate,"yyyyMMdd",CultureInfo.CurrentCulture);
polDS.Policy[0].DEPARTUREDATE = MyDate.ToString("MM/dd/yyyy");

Is there a way to create a method that I could pass in strDate so that it's converted? That way I can just call the method for each substring that I need converted.

ConvertDate(line.Substring(296,8);

Thank you


0
Comment
Question by:boukaka
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
apresto earned 300 total points
Comment Utility
Hi boukaka,

public DateTime convertDate( string strDate )
{
  DateTime MyDate = DateTime.ParseExact(strDate,"yyyyMMdd",CultureInfo.CurrentCulture);
  return MyDate;
}

then just call it like this:

string strDate = line.Substring(296,8);
DateTime MyDate = convertDate( strDate );

Apresto
0
 
LVL 23

Expert Comment

by:apresto
Comment Utility
is this what you meant?
0
 

Author Comment

by:boukaka
Comment Utility
That's exactly what I meant! Thank you - sorry for the delay, went for lunch.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

12 Experts available now in Live!

Get 1:1 Help Now