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.
Unfortunately, activation email could not send to your email. Please try again.

Excel Cell to contains time value only

Thread ID:

Created:

Updated:

Platform:

Replies:

73837 May 21,2008 11:26 PM Jun 10,2008 05:58 AM Windows Forms 5
loading
Tags: XlsIO
Lim Kek
Asked On May 21, 2008 11:26 PM

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.



Bhuvaneswari P [Syncfusion]
Replied On May 22, 2008 02:16 AM

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


Lim Kek
Replied On May 22, 2008 04:11 AM

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.


Bhuvaneswari P [Syncfusion]
Replied On May 23, 2008 07:12 AM

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




Administrator [Syncfusion]
Replied On June 9, 2008 11:28 AM

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


Yavanaarasi G [Syncfusion]
Replied On June 10, 2008 05:58 AM

Hi Ami,

Thank you for sharing your ideas.

Please let me know if you have any other concerns.

Regards,
G.Yavana


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;