tags:

views:

1268

answers:

7

Hi,

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 ???

A: 

1: read this

2: is the column is a datetime or varchar?

Fredou
A: 

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.

Khalid Abuhakmeh
+5  A: 
  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)

Rowland Shaw
+1, but I want to also emphasize that all data should be stored in the program as a datetime type. Only use string when reading from and showing to the user.
Joel Coehoorn
actually, it's should be 'MMM' i think. mmm doesn't exist? mm is for minutes/time. i think the case is important for the string.
Pure.Krome
Pure.Krome is right
Lucas
+2  A: 

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.

Pure.Krome
A: 

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.

Mladen Mihajlovic
4x m? and lowercase, not upper case? might want to check that, mate :)
Pure.Krome
Ok amended... :)
Mladen Mihajlovic
+1  A: 

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
Guffa
A: 

Hi,

Thanks for ur replies... I went 4 a code like this... <**pay_period** is of *Datetime*

type>

try { CultureInfo MycultureInfo=new CultureInfo("en-US");

       pay_period=DateTime.ParseExact(c.Value.ToString(),"MMM-yy",MycultureInfo);
     }

     catch(Exception ex)
     {
     Label1.Text="Error";
     }

now i want to use this variable pay_period to compare with DB date values (say a column "tim_per" having a value '20-Jul-1998' )

How do i do tht as pay_period is of 'MMMM-yy' format. and it's showing invalid month when i compare both in SQL Query ( select * from datess where tim_per<=pay_period )

Is ParseExact() method sufficient ????

Sry!! I dont get Guffa's answer clearly too....

URGENT!!!

stack_pointer is EXTINCT