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

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

Related

How to use built-in date formats with 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";

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 TEXT dd/mm/yyyy in SQL column to DATE YYYY-MM-DD

I would love to know the best way to handle data that has been inputted incorrectly as dd/mm/yyyy into a sql database as TEXT and to have it converted into a new column of the table with the datatype as DATE so it is actually stored as yyyy-mm-dd.
Existing text date column name is called "olddate" with an empty column created called "truedate" to house the new data. Each row has the date field, but none are able to be sorted correctly because of this issue.
Any ideas how I can slice and dice the current date into a new DATE field friendly version?
Thanks in advance :-)
That is style 103. So use:
select convert(date, col, 103)
Are you using Oracle? If so, TO_DATE is what you want. You can take in a string that represents a date and convert it to a date using the format you pass it.

Incrementing a date in openrefine

I have a date in format of YYYY-MM-DDThh:mm:ss
Please provide a GREL expression that increments date to 1 month from the present date value for all cells in the column in openrefine. Thanks!
First you need to make sure the data in the cells is of type 'date' - if the text in the cell is in green then the data is already 'date' type. Otherwise you will need to convert it using the GREL:
value.toDate()
Screenshot - before converting to date
Screenshot - after converting to date
Once you have the data as Date type, then you can use the following GREL to increment by one month:
value.inc(1,'month')
For more on using dates in OpenRefine see https://github.com/OpenRefine/OpenRefine/wiki/GREL-Date-Functions

how to get excel to treat a date as a date not a string when doing CopyFromRecordset

I have an SQL query from SQL Server which returns dates as a string in the format "YYYY-MM-DD".
If I enter a date in this format into a cell, it's recognised as a date.
But when I populate a worksheet with CopyFromRecordset, it seems to be treated as a string.
Any formula which uses the cell converts it to a date first. For example, if my dates are in col A and I make a new column B filled with a formula =A1 + 0
the formula returns my date, as a date.
The problem:
I use the Recordset data for a few things, one of them being a pivot table.
The pivot table does not see my dates as dates. I can't group as dates, for example. My hack is to make a new column which is basically =A1 + 0
I'm going to change my macro to automate this adding a zero, but I wonder if there's a way to get it right from the moment the CopyFromRecordset is performed.
The easiest way would be to do the conversion on the SQL server e.g.
SELECT CAST(date_text AS DATE) FROM TestExcelDates;
CopyFromRecordset is well known for causing data type / cell formatting issues in Excel.
I think I remember reading somewhere this is because the datatype of the recordset is ignored and Excel attempts to work out the format of each column itself based on a subset of the data in the recordset.
The best way round this is to set the cell formatting in the destination range before performing the CopyFromRecordset.
I had this problem after I had changed a view on my SQL Server database. I had changed the data type to DATE; formerly it was on an older version which didn't support DATE so I had used DATETIME. I suspect Excel doesn't always recognize the Date datatype through the SQLOLEDB provider, but it does recognize DATETIME. The field of interest is meas_date. So I altered the view by changing this to a cast SELECT CAST(meas_date AS DATETIME) AS meas_date, ... and refreshed the query in Excel. Worked!
Use the CDate() function when populating cells with dates from the recordset. This will convert the string to a date value.
Edit
That works for setting individual cell values. For using CopyFromRecordset I think you need to do the conversino in the SQL query, so the column returned by the query is a date type rather than a string.
I had this problem too importing data from Teradata, and got around it by first formatting the date columns with NumberFormat = "m/d/yy h:mm;#" (24 hr date) then stepping through the date fields afterwards with VBA and doing ws.cells(iRow, iCol).value = ws.cells(iRow, iCol).value, it forces Excel to reevaluate the string into a date/time field.
This probably will not be the answer but will surely helps you finding the right solution for your problem
String stringCellValue = myCell.toString();
here myCell has datatype as CELL which I've converted to String format.
If u want it in desired Date format, then u can try this-
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");
myCellDate = sdf.parse(stringCellValue );
Hope it helps in solving your problem...