We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Excel Cell to contains time value only

The code:

worksheet.Range(XRow, XCol).Value = "12:45:12"

will create a cell in excel with Date & time where the date is defaulted to today date.

How could i modify the code so that the cell in excel will stored only time with no date? Excel does allows cell to have time only value.



5 Replies

BP Bhuvaneswari P Syncfusion Team May 22, 2008 06:16 AM UTC

Hi Limkek,

Thank you for your interest in Syncfusion products.

Yes, it is possible to display Time alone in the cell by setting the custom format. Please refer the below code snippet to do so:


IWorksheet sheet = ExcelWorkbook.Worksheets[0];
//Assign today DateTime
sheet.Range["A1"].DateTime = DateTime.Now;
//Set the time format to display Time only
sheet.Range["A1"].NumberFormat = "HH:MM:SS AM/PM";



Please try this and let me know if this helps you.

Best Regards,
Bhuvana



LK Lim Kek May 22, 2008 08:11 AM UTC

Currently i am using the same method that you suggested. Yes, the cell show time only format but the actual value also include today date. Cell in Excel actually allows us to store time value only. It will be very different when this cell is refered by other cell for formula calculation, the result will not be same.



BP Bhuvaneswari P Syncfusion Team May 23, 2008 11:12 AM UTC

Hi Limkek,

Thanks for the update.

If you want the Time alone please use the DisplayText property or if you want both Date and Time then please use Value property of the cell.


//This will return both Date and Time
sheet.Range["A1"].Value;

//This return only the display text, in this case it will return the Time alone.
sheet.Range["A1"].DisplayText;



Please try this and let us know if this helps you.

Best Regards,
Bhuvan





AD Administrator Syncfusion Team June 9, 2008 03:28 PM UTC

There several ways to do it:

1) Excel stores time and date in the same way, so there is no difference for between them, just number formatting. You just have to set correct year, month, day.
2) alternatively you can use TimeSpan property from IRange to assign TimeSpan object
3) set correct number ( 1 means 1 day, so 1 hour = 1/24) and assign number format



YG Yavanaarasi G Syncfusion Team June 10, 2008 09:58 AM UTC

Hi Ami,

Thank you for sharing your ideas.

Please let me know if you have any other concerns.

Regards,
G.Yavana


Loader.
Live Chat Icon For mobile
Up arrow icon