softartisans excelwriter - How do I get a date style of dd-mm-yyyy hh:mm:ss?
-
02-07-2021 - |
Question
I can either use NumberFormat.DateFormat.MonthDayYearHourMinute24
or NumberFormat.TimeFormat.HourMinuteSecond24
but nothing that will give me the date and the time together along with the seconds :(
Solution
[Disclaimer: I am the product owner of OfficeWriter]
If you need a custom date format that is not included in the list of built-in NumberFormat.DateFormat enumerations, you can set Style.NumberFormat to any formatting string you want. In this case, the formatting string needs to be modified to include the second variables:
//Get NumberFormat
string numFormat = style.NumberFormat;
//Set NumberFormat
style.NumberFormat = "m/d/yy h:mm:ss AM/PM";
The formatting strings accepted by OfficeWriter are the same as those in Excel. To find a new formatting string, you can look up the number format in Excel under Format Cells > Number. Select 'Custom' to find the exact formatting string for a particular number format. More information is available in the OfficeWriter documentation under Number Formats.