Custom date formats

Dear Aspose support,


I would like to ask help regarding date formats.

I know it is possible to set date format using predefined formats as discussed in this page:
Format cells|Documentation

But I am curious if it would be possible to use a format that is not listed.
So if I would like to put a date in a cell and would like to format the date like this for example:
yyyy-mm-dd, how can I do that?

Kind regards,

Tamas


Hi Tamas,


Thank you for contacting support.

You may use the Style.Custom property to specify the desired pattern, and set the style to any cell in order to get it formatted as per requirement. Please check the below provided piece of code and attached spreadsheet for your reference.

C#

//Instantiat a Workbook object
Workbook workbook = new Workbook();

//Input some data in A1 cell
workbook.Worksheets[0].Cells[“A1”].PutValue(DateTime.Now);

//Get the style of A1 cell
Style style = workbook.Worksheets[0].Cells[“A1”].GetStyle();

//Set custom format to yyyy-mm-dd
style.Custom = “yyyy-mm-dd”;

//Set style to the A1 cell
workbook.Worksheets[0].Cells[“A1”].SetStyle(style);

//Save the result
workbook.Save(myDir + “output.xls”);

Please feel free to write back in case you face any difficulty.