Click here to Skip to main content
16,020,974 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I m storing datetime from a calendar extendar and storing it in the database.The Format of the datetime is Format="dddd, MMMM dd, yyyy". Then i m displaying this Datetime with other field in a grid view and naming this field as 'CalendarDate'. Currently the CalendarDate in the grid is displaying like "6/29/2012 10:42:35 AM".

I want that the Calendar date will display Date like this:-"6/29/2012 10:42 AM". Only seconds will be removed.Please advise me that how i do this.

The Stored Procedure which i m using now is like this:-
SQL
Create procedure St_Proc_GetUserReportforCurrentDayTask @userID int as
Begin
set NoCount on;
DECLARE @TODAY DATE
SET @TODAY = CONVERT(VARCHAR(10), GETDATE(), 111)
select Production.CalendarDate as Date, RegionAndProjectInfo.RegionProjectName as Region , County.CountyName as County, WorkType.WorkTypeName as WorkType, Task.TaskName as Task,Production.VolumeProcessed as 'Volumes Processed', Production.TimeSpent as 'Duration (HH:MM)' from Production
inner join RegionAndProjectInfo
on
RegionAndProjectInfo.RegionProjectID=Production.RegionProjectID
inner join County
on
County.CountyID=Production.CountyID
inner join WorkType
on
WorkType.WorkTypeID=Production.WorkTypeID
inner join Task
on
Task.TaskID=Production.TaskID
where Production.UserID=@userID and CalendarDate >= @TODAY
End
Posted

1 solution

Hello,

PLease go through below link:
http://hamidseta.blogspot.in/2011/06/datetime-format-in-gridview.html[^]
http://www.aspdotnet-suresh.com/2011/05/how-to-set-date-format-in-gridview.html[^]

For you requirement, it will be like below:
<asp:boundfield datafield="Date" dataformatstring="{0:g}" headertext="CalendarDate" readonly="True" > </asp:boundfield><br />


From SQL Server, point of view, try below query while fetching the date:
SQL
SELECT CONVERT(VARCHAR,GETDATE(),101) + RIGHT (CONVERT(VARCHAR,GETDATE() , 100 ) ,7)

Result will be like :
MM/dd/yyyy HH:mm AM/PM.
 
Share this answer
 
v3
Comments
Dharmenrda Kumar Singh 22-Jun-12 4:13am    
The Problem is solved but now it is displaying two columns in Grid, One with the Formatted Date with Column name as "CalendarDate" and One more with Column name as "Date" containing unformated Date.
Actually i m executing the stored procedure and storing the information to a dataset and then binding that dataset to the grid view.
Vani Kulkarni 22-Jun-12 5:54am    
You need not select the Date again in your stored procedure, as you have already formatted it. Otherwise you can hide the column in gridview, by setting the visibility to False.
Dharmenrda Kumar Singh 22-Jun-12 5:56am    
Yes i had done the same and in SQl Server i had made the changes.
Thanks a lot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900