stack_pointer is EXTINCT
stack_pointer is EXTINCT

Reputation: 2393

String to mmm-yy format of time in C#

I need to perform some date operations in ASP.net using C#.

  1. The date i would enter should be of format 'Jul-05' (mmm-yy Format and type-string)... how can i check with this???? Or how can i validate this with whatever user is entering as a string???

  2. After validating that, i need to compare tht with a value in Database(say a column name buy_period which has a value (say) 04/31/2007).

How can i write a Query for comparing both?? (as both dates would be of different formats)

Can u pls help me in this ???

Upvotes: 0

Views: 7670

Answers (6)

Rowland Shaw
Rowland Shaw

Reputation: 38130

  DateTime myDateTime = DateTime.ParseExact( input, "MMM-yy" );

You can then happily pass it to a stored procedure (etc.) as a parameter to do your comparison on the server (or just use the DateTime returned as the result of an existing query)

Upvotes: 5

Mladen Mihajlovic
Mladen Mihajlovic

Reputation: 6435

You could use

DateTime date = DateTime.ParseExact(value, "MMM-yy", null); //checked at http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

and then use that date in a sql command parameter.

Upvotes: 0

Guffa
Guffa

Reputation: 700152

Use the TryParseExact method to validate the string and parse it to a DateTime value:

DateTime month;
if (DateTime.TryParseExact("MMM-yy", CultureInfo.InvariantCulture, DateTimeStyles.None, out month)) {
   // parsing was successful
}

The DateTime value will use the first day of month and the time 0:00 to fill up a complete value, so a string like "jul-05" will be parsed into a complete DateTime value like 2005-07-01 00:00:00.0000, so it will be the starting point of that month.

To compare this to a date in the database you also need the starting point of the next month, which you get with:

DateTime nextMonth = month.AddMonths(1);

Now you can just compare a date to the starting and ending point of the month in this manner:

where date >= @Month and date < @NextMonth

Upvotes: 2

Pure.Krome
Pure.Krome

Reputation: 86937

The .NET framework has some nice methods on the DateTime struct :: Parse, TryParse, ParseExact, TryParseExact.

This info is discussed on MSDN.

Becuase you're providing a custom date string, we should then use the ParseExact or TryParseExact. The later doesn't throw an exception if it fails to parse.

So.. lets try this...

using System.Globalization;

CultureInfo MyCultureInfo = new CultureInfo("en-US");
string myString = "Jul-05";
DateTime myDateTime = DateTime.ParseExact(myString, "MMM-yy", MyCultureInfo))
Console.WriteLine();

the value myDateTime can then be passed to a database as a DateTime property and checked against that.

EDIT: Damn, beaten by Rowland by a min, as i was typing it!

EDIT 2: Please note the "MMM-yy". As stated on the MSDN page, MMM is "Represents the abbreviated name of the month as defined in the current System.Globalization.DateTimeFormatInfo.AbbreviatedMonthNames property." mmm (lower case) is invalid.

Upvotes: 2

Khalid Abuhakmeh
Khalid Abuhakmeh

Reputation: 10839

well your validation and comparison have to be two different operations. so you could do alot of things for validation.

Validation Options:

1.) Split your string on "-" and check to see if the mmm part is in your list of months, and then check to see if the number is valid.

2.) Regular Expression, this is advanced but can be reduced to one line. Look up RegEx if you are interested.

After you've validated the string, convert it to a DateTime object and compare it to the other value using DateTime.Compare().

Hope that helps.

Upvotes: 0

Fredou
Fredou

Reputation: 20090

1: read this

2: is the column is a datetime or varchar?

Upvotes: 0

Related Questions