views:

98

answers:

2

I need to add the current date time in a bigint field in a database... and then display from that only the date in format: october 1, 2009.

I am currently thinking of storing the value in string variable and then converting it to int... String s = DateTime.Now.ToString();

i dont know what to do next.. please help

+4  A: 

You could just store the number of ticks as your bigint value. Ticks represent the number of elapsed 1/10,000 of milliseconds since January 1, 0001.

DateTime.Now.Ticks;

This can always be converted back to a DateTime by using the constructor that accepts a long:

DateTime storedTime = new DateTime(ticksFromDatabase);

To format your date, just use any of the standard date format strings. A custom format string might work better actually, I just perused them and it doesn't look like there's a built in one for the format you want. This should work:

date1.ToString("MMMM d, yyyy", CultureInfo.CreateSpecificCulture("en-US"))
womp
i stored it aslong s= DateTime.Now.Ticks;but i am using a SQL datasource to display it in a gridview.What should i write in the select query...???My SQL query is.."SELECT MachineGroups.MachineGroupID, MachineGroups.MachineGroupName, MachineGroups.MachineGroupDesc, MachineGroups.TimeAdded, MachineGroups.CanBeDeleted, COUNT(Machines.MachineName) AS 'No. of PCs' FROM MachineGroups FULL OUTER JOIN Machines ON Machines.MachineGroupID = MachineGroups.MachineGroupID GROUP BY MachineGroups.MachineGroupID, MachineGroups.MachineGroupName, MachineGroups.MachineGroupDesc, MachineGroups.TimeAdded
You'll need to hook into the RowDataBound event and reformat the date. Is there any reason you can't use a sql DateTime column? That would be the easiest thing to do, since you could format it both with SQL select statements and easily transfer it to DateTime objects in c#.
womp
no i cant use datetime coz the entire company code is in bigint..:(I kno it would have been so easy for me too..But i seriously did not get how to display the date in a gridview using the select statement...Please help me man...
i m not able to figure out how to use dateboundwhat o do with thisDateTime dt1 = DateTime.Parse(e.Row.Cells[3].Text); e.Row.Cells[3].Text = dt1.Date.ToString();
+1  A: 

I'd use a smart date key, since it's easier to find that using SQL:

20090927235000
yyyyMMddhhmmss

This way, if you want to find anything that happened on a given day, you could do:

select * from tbl where datecol between 20090927000000 and 20090927240000

Thereby making data validation a lot easier, even if you are using an ORM.

Eric