Showing posts with label Date. Show all posts
Showing posts with label Date. Show all posts

Wednesday, October 9, 2013

CONVERT DATETIME seems all right, but still ...

A table with a varchar column that has date and other value types too.
A query with a simple CONVERT DATETIME

Conversion failed when converting date and/or time from character string.
Crashing. Over and over again. I start to call it X-Files.
Until I read THIS.

Not only I know why it happened (and sortof feel like SQL is "stupid" :D) but I get an interesting idea to work around the issue.

Friday, September 19, 2008

Monthname in C# (C Sharp)

I think there's no Monthname (like is asp for ex. - which is weird)

But this is easy:

DateTime swTest = new DateTime(2000, 11, 1);
Page.Response.Write(swTest.ToString("MMMM"));

Thursday, July 3, 2008

C# convert mm/dd/yyyy string to "5 minute(s) ago"

this is how utube does it:



For a forum-kinda thing I wanted to (C#) convert mm/dd/yyyy string to "5 minute(s) ago" - like Youtube does.

Here's how u I did it:
(please feel free to comment, get me a better way to do this :D)





/// < summary>
/// transform into "9 hours ago" or "8 days ago" etc ...
/// < /summary>
/// < param name="makeThisNiceDateFormat">some date string ... "mm/dd/yyyy"< /param>
/// < returns >example: "5 minute(s) ago"< /returns>
public static string ConvertDateToHoursAgo(string makeThisNiceDateFormat)
{
if (!IsDate(makeThisNiceDateFormat))
{
return makeThisNiceDateFormat;
}
else
{
DateTime dtOriginal = DateTime.Parse(makeThisNiceDateFormat);
//check if we need to display minutes
if (Math.Abs(DateDiff(DateInterval.Minute, dtOriginal, DateTime.Now)) < 60)
{
if (DateDiff(DateInterval.Minute, dtOriginal, DateTime.Now) == 0)
{
return "just now";
}
return (Math.Abs(DateDiff(DateInterval.Minute, dtOriginal, DateTime.Now)) + " minute(s) ago");
}
//check if we need to display hours
if (Math.Abs(DateDiff(DateInterval.Hour, dtOriginal, DateTime.Now)) < 24)
{
return Math.Abs(DateDiff(DateInterval.Hour, dtOriginal, DateTime.Now)) + " hour(s) ago";
}
//check if we need to display days
if (Math.Abs(DateDiff(DateInterval.Day, dtOriginal, DateTime.Now)) < 31)
{
return Math.Abs(DateDiff(DateInterval.Day, dtOriginal, DateTime.Now)) + " day(s) ago";
}
//check if we need to display months
if (Math.Abs(DateDiff(DateInterval.Month, dtOriginal, DateTime.Now)) < 12)
{
return Math.Abs(DateDiff(DateInterval.Month, dtOriginal, DateTime.Now)) + " month(s) ago";
}
return Math.Abs(DateDiff(DateInterval.Year, dtOriginal, DateTime.Now)) + " year(s) ago";
}
}





thoughts?
Note: I got an IsDate function in there which returns true/false ... in case someone tries to send me something that is not a date ...