Click here to Skip to main content
15,886,724 members

Comments by PopeDarren (Top 19 by date)

PopeDarren 18-Feb-22 16:35pm View    
You're very welcome!
PopeDarren 24-Aug-16 18:44pm View    
I haven't been on this site in a while... sorry for the late response. You really should start a new question and not reply to another question in order to get a good answer.
Well... again, I'd be making a bunch of assumptions...
Below is a test console project I threw together to test formatting dates in C#. From your other replies on this message, it looks like you're in C# and not SQL. Please note that this is NOT a complete solution. This could cause dates to end up in the wrong format and you won't know until looking at the data later, which is, obviously, not good. For example: 10/11/12 will return a date for all three formats found below (Oct 11 2012, 10th November 2012, 2010 November 12). If at all possible, you'll need much more business logic in order to get this one right... and you'll still probably get junk values. However, IF they're just in the two formats you talked about above (yyyy/MM/dd and dd/MM/yyyy) that makes it much easier. You can take the MM/dd/yyyy format out of the code below and just use the other two. You can also replace the date format arrays with just the single formats if your years are always four digits.

static void Main(string[] args)
{
string one = "12/22/2015";
string two = "22/12/2015";
string three = "2015/12/22";

DateTime dateOne, dateTwo, dateThree;

dateOne = ConvertDate(one);
dateTwo = ConvertDate(two);
dateThree = ConvertDate(three);

if (dateOne == dateTwo)
Console.WriteLine("Dates one and two are equal.");

if (dateOne == dateThree)
Console.WriteLine("Dates one and three are equal.");

if (dateTwo == dateThree)
Console.WriteLine("Dates two and three are equal.");
}

public static DateTime ConvertDate(string inVal)
{
DateTime dateValue;
if (DateTime.TryParseExact(inVal, new string[] { "MM/dd/yyyy", "MM/dd/yy" }, null, DateTimeStyles.None, out dateValue))
{
Console.WriteLine("American Date: {0} to {1}",
inVal, dateValue.ToString());
return dateValue;
}
else
{
if (DateTime.TryParseExact(inVal, new string[] { "dd/MM/yyyy", "dd/MM/yy" }, null, DateTimeStyles.None, out dateValue))
{
Console.WriteLine("Great Britain Date: {0} to {1}",
inVal, dateValue.ToString());
return dateValue;
}
else
{
if (DateTime.TryParseExact(inVal, new string[] { "yyyy/MM/dd", "yy/MM/dd" }, null, DateTimeStyles.None, out dateValue))
{
Console.WriteLine("Japan Date: {0} to {1}",
inVal, dateValue.ToString());
return dateValue;
}
else
{
Console.WriteLine("fail");
throw new Exception("Date does not fit known format.");
}
}
}
}

HTH!
PopeDarren 14-Aug-14 15:00pm View    
What's with the down votes? It solved his problem.
PopeDarren 16-Jul-12 11:48am View    
I'm flattered that you think I could solve more problems, but the truth is that I've just wrestled with this problem before. There are people on here that are far more knowledgeable than me.
PopeDarren 13-Jul-12 12:31pm View    
Whoops! What I said about Option 2 was wrong! You don't have to add the runat="server" attribute, you just need to add a name.

<input type="text" id="datepicker_value" name="datepicker_value" style="border:none; " />

Then in your button click:

Me.update_lbl.Text = Request.Form("datepicker_value")