How to use built-in date formats with EPPlus - epplus

I need to display the first letter of the month for a date value in a cell. In Excel I can select the M format under date (see below). How can I programmatically set that format using EPPlus in C#?

The number format to get the first letter of the month is mmmmm (five m's). You can set the format for cells using ExcelRange.Style.Numberformat.Format.
Example:
ws.Cells["A1"].Value = DateTime.Today;
ws.Cells["A1"].Style.Numberformat.Format = "mmmmm";

Related

VBA date format code

I was wondering if there is any possibility to write a VBA code where the column A should always have a date format like this: 12.10.2017 (not 12/10/2017 or 12-10-2017). If anything else is written in the column A like "12" or "car" the entry should be deleted. It has to accept only the date format mentioned above.
I used data validation for this, with length 10 and the date format to take only "." into consideration, but I want to do it as a VBA code instead.
Thanks!
A valid date is a long representing the number of days since the 1st january 1900. So a valid date would be 45603. You can display this date in any format you wish using the format codes d, m and y . So to display the date as dd.mm.yyyy then set that numberformat in the cells in column A. Your problem though is that Excel will only accept a date entered as either a long or in a built in date format (using /, - or space as a separator). You could allow the users to enter a text string in the format dd.mm.yyyy and then convert that string into a date and then reject it if the conversion didn't result in a valid date - but wouldn't it be easier to just train your users to enter dates correctly?

Convert number in textbox to month name for vba statements

I have a textbox wherein I input number as months (like "01" for January) but I need the actual month name instead of the number in my VBA statements. Can you please help me convert the number in the textbox into Actual Month Name.
I have tried the statement below but it is not working:
Dim MoName As String
MoName = Format(Month(Monthtxtbx.Text), "MMMM")
Range("Cells(1,1):Cells(LastCol, Lastcolumn)").AutoFilter Field:=3, Criteria1:=MoName
Use MoName = MonthName(Monthtxtbx.Text) to get the month name.
Alternatively, you could dummy up a date so that you could use the "mmmm" format as:
MoName = Format(DateSerial(2000,Monthtxtbx.Text,1), "MMMM")
but that would be the long way to achieve something simple.

PHPExcel - set cell format to "Date (short)" and insert date from string

I have an .xlsx file where cells of column C have format Date (short). In the cells is visible a date in this format: 30.1.2017.
If you switch the cells format to General you will see numbers like this: 42765.
I need to insert new line, set format Date (short) on C cell of new column and insert the date that I have in string. Cannot find approriate examples. How could it be done?
MS Excel stores dates as a serialized timestamp value, a count of the number of days since 1st January 1900 (or 1st January 1904 if created on a Mac)... that's what your 42765 value is.
PHPExcel provides a number of functions for date handling, which can be found in the PHPExcel_Shared_Date class.
You can convert a unix timestamp or a PHP DateTime object to an MS Excel serialized timestamp using the PHPToExcel() method. If you have a string value for your date, then convert it to a PHP DateTime object first.
Then you can set the cell value to the resulting timestamp value, and apply a number format mask to tell Excel how that date should be displayed. The 02types.php example in the Examples folder demonstrates this:
$dateTimeNow = time();
$objPHPExcel->getActiveSheet()
->setCellValue('C9', PHPExcel_Shared_Date::PHPToExcel( $dateTimeNow ));
$objPHPExcel->getActiveSheet()
->getStyle('C9')
->getNumberFormat()
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2);
And it is also explained in the PHPExcel Documentation

VB.Net: Use different date format from Excel sheet while importing

From an Excel sheet I import to MSAccess database. When I read the date column from the Excel sheet, want to convert it to known date format.
I get two Excel formats, one with dd-mm-yyyy format and another with mm/dd/yyyy.
When I import Excel sheet with date 9/14/2015, I get an error message "string was not recoganized as valid date time".
timesheetDate = xlWorkSheet.Cells(1, Column).value
tempDate = Convert.ToDateTime(timesheetDate)
How to convert to two different formats of date to one format?
Usually I using the two properties: "ToOADate" and "FromOADate" when converting or moving data with no problems.
For sample: if you want to read a date from Excel to your form
Convert cell of date in Excel to number.
In form: convert the number to a date.May this short script help you:
Dim DateAsNumber As Integer
DateAsNumber = Int(Now.ToOADate)
MsgBox(DateAsNumber) 'if now is 13-Apr-2016 will you get 42473
Dim MyDate As Date
MyDate = DateTime.FromOADate(DateAsNumber)
MsgBox(MyDate) 'this will convert 42473 to 13/4/2016 or 4/13/2016 depending on your system date format.

Changing date formats in Excel using

I have a excel sheet in which a column has date date in the format "yyyyMMdd" and I want to format it as "yyyy/MM/dd".
For this I tried to use following line inside macro, but it's converting cell data as "###.....#" instead of changing date format.
Sheet1.Range("C3", "C302").NumberFormat = "yyyy/mm/dd"
...
result = "#####...#"
...
Can someone tell me why it's happening? Is there any other way for doing this?
If a date/time cell appears full of # signs, it means that the column is too narrow to display the format.
Make the column wider to accommodate the full width of the selected date format.
See this screenshot. Both columns have the same format. Column A is too narrow to show the dates. Column B is wide enough.
Edit after discussing in chat:
The screen shot you posted in chat is this:
The "dates" you are referring to are not dates. They are numbers that are way higher than what Excel uses for dates in this millenium.
Excel stores dates as whole numbers, starting as 1 for 1/1/1900. What you show in your screenshot are numbers way higher than Excel dates.
Your number 20150930 is NOT what Excel considers Sep-30-2015. For Excel, that date would be the number 42277, which you can perfectly format as that date.
The reason that your "dates" formatted with your format string come out as ##### is that the numbers are way higher than what Excel can interpret as dates.
You will need to convert your numbers to real Excel dates, which you can do with a simple formula. With your first "date" number in cell A1, you can use the formula
=DATE(LEFT(A1,4),MID(A1,5,2),RIGHT(A1,2))
to return a value that Excel regards as a true date for Sep-30-2015 in this screenshot:
So, the reason for all the # signs is that the numbers you are trying to format as dates are too big for dates in Excel's algorithms.
With all the good answers, I will add simple vba solution...
Option Explicit
Sub FormatDate()
Dim xlRng As Range
Dim xlShtRng As Range
'//- Date format 20160112
Set xlShtRng = [A3:A10] '//- or [A3, A6, A10]
For Each xlRng In xlShtRng
xlRng.Value = DateSerial(Left(xlRng.Value, 4), Mid(xlRng.Value, 5, 2), Right(xlRng.Value, 2))
xlRng.NumberFormat = "yyyy/mm/dd" '//- 2016/01/12
Next
End Sub
Please try this..
=LEFT(A1,4)&"/"&MID(A1,5,2)&"/"&RIGHT(A1,2)