I am using phpexcel to read specific set of data from a excel sheet into an array.
// example $cell_range = 'A2:AH35';
$cell_collection = $objPHPExcel->getActiveSheet()->rangeToArray($cell_range, NULL, True, TRUE);
In this sheet, two columns (L & N) contain dates formates as dd-mmm-yy (01-Jan-16). In order to upload this into database I need to convert this to format yyyy-mm-dd (2016-01-01).
I cannot simply change the last parameter of rangeToArray to FALSE (formatting retention), because that will just give me a set of x numbers instead of the date.
So how do if reformat the dates? Can be either on the level of phpexcel reading the data or by manupulating the resulting $cell_collection array.
You need to do it manually.... you know which columns contain dates, so return the "raw" MS Excel timestamp and use PHPExcel_Shared_Date::ExcelToPHP() or PHPExcel_Shared_Date::ExcelToPHPObject() to convert to a unix timestamp or DateTime object, and then you can use standard PHP date() function or the DateTime object's format() method to convert to a human-readable date string in whatever format you want.
Alternatively, reset to MS Excel numberformat mask to whatever date/time format you ant for those columns/cells before calling toArray().
Related
Data
I am trying to create a time dimension using this:
t1 = datetime(1901,1,1);
t2 = datetime(2016,12,31);
t = t1:t2;
And create a netCDF file using this
nccreate('prec.nc','Prec',...
'Dimensions',{'time' 42369 'lon' 135 'lat' 129},...
'Format', 'netcdf4');
What I have tried
ncwrite('prec.nc', 'time', t);
Error Message
Error using cast
Unsupported data type for conversion: 'datetime'.
Error in internal.matlab.imagesci.nc/write (line 778)
scale_factor = cast(1, class(varData));
Error in ncwrite (line 87)
ncObj.write(varName, varData, start, stride);
Question
How can I create a daily time dimension that I can write out to a netCDF file? What is the proper date type for this conversion?
NetCDF doesn't define a single native way of storing date/time values, but there are established conventions, as desribed here.
There are two strategies for storing a date/time into a netCDF variable. One is to encode it as a numeric value and a unit that includes the reference time, e.g. "seconds since 2001-1-1 0:0:0" or "days since 2001-1-1 0:0:0" . The other is to store it as a String using a standard encoding and Calendar. The former is more compact if you have more than one date, and makes it easier to compute intervals between two dates.
So you could:
a) Use datestr to convert it to a string value. The conventional date string format for data interchange is ISO 8601, which you can get in Matlab with datestr(myDateTime, 'yyyy-mm-ddTHH:MM:SS').
b) Convert it to a numeric value representing seconds or days since a reference "epoch" time. I'd suggest using the Unix epoch, since Matlab provides a convenient conversion function for this already: posixtime(myDateTime). Then specify your units for that variable in the NetCDF file as 'seconds since 1970-01-01 00:00:00'.
You probably want to make sure your datetimes are in UTC before encoding them in the NetCDF, so you don't have to worry about time zone issues.
Would you be able to advise how can i change the format of the date the functions isbusday and busdate are using ?
The functions use US date format by default, but I need them to be in European format dd/mm/yyyy.
I have attempted to use the code below but it is not working.
isbusday('01-01-2015','dd-mm-yyyy')
busdate('01-01-2015','dd-mm-yyyy',1)
Thank you very much.
You want to convert your string to a datetime object. That is where you can control the format:
d = datetime('01-01-2015','InputFormat','dd-mm-yyyy');
isbusday(d)
busdate(t)
See the documentation: https://www.mathworks.com/help/matlab/ref/datetime.html and https://www.mathworks.com/help/finance/isbusday.html
I use a date picker that saves the date in a yyyy-mm-dd format in a database. It then automatically adds time that always appears as 00:00:00. So for example it displays the date like this : 2014-12-14 00:00:00. I want it to display just the date in a mm-dd-yyyy format.
I use the code below but something seems to be wrong with it because it simply doesn't change the way it is displayed. I want to split up each of the values, then display only the date in a different format.
var parts = value.split("/");
return parts[1] + "-" +parts[2] + "-" +parts[0];
What can I do to make it work? Javascript please.
Thanks in advance.
It's a wide variety of solutions. Depends on you server-side settings too, but I will assume, that you use common for most websites MySQL DB and PHP.
You can change you column type to DATE. As said in docs:
The DATE type is used for values with a date part but no time part.
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The
supported range is '1000-01-01' to '9999-12-31'.
You can change your column type to VARCHAR and store a date in any format you like when INSERT it
You can convert it to proper format using MySQL built-in DATE_FORMAT()
SELECT DATE_FORMAT('2014-12-14 00:00:00', '%c-%e-%Y');
will give you 12-14-2014. Here is sqlfiddle,
just change first param to your column name in query
On server you can use PHP's date() function like this
$yourdate = '2014-12-14 00:00:00';
echo date("m-d-Y", strtotime($yourdate));
Closer to your question, how to do it in JS? It also has special object for this: Date(). Put your date in constructor, then use methods:
var yourdate = new Date('2014-12-14 00:00:00');
alert(yourdate.getMonth() + "-" + yourdate.getDate() + "-" + yourdate.getFullYear());
JSFiddle < - here
So, as far as almost every platform understands this datetime format - you can use any tool for converting it
I am using the jPlaton platform and I have a PlatonScript record that contains a date and a time field.
The date field is a integer like 20140526.
I want to convert it to a YYYY-MM-DD string. That it: 20140526 --> "2014-05-26"
The time field is a integer like 90903417.
I want to convert it to an HH:mm:ss string. That it: 90903417 --> "09:09:03"
Any ideas?
Thanks
The basic functions you need are:
ASDATE (for integer to string transformation)
example: #s_dateFromInteger# = (ASDATE:YYYY-MM-DD) #s_integerDate#
ASTIME (for long to string transformation)
example: #s_timeFromLong# = (ASTIME:HH:mm:ss) #s_longTime#
In these examples #s_dateFromInteger# and #s_timeFromLong# are the Strings you require.
s_integerDate is the classic integer date representation used by jPlaton (yyyyddmm).
s_longTime is the classic long time representation used by the platform (hhmmssSSS)
Don't forget that the date and time patterns used by Platonscript are the usual JAVA patterns, since Platonscript interpreter returns pure JAVA
i can get my datetime populated as 2012-07-24 15:31:21
However, its not in the format that i want. This is my current query
SELECT CONVERT(datetime2(0), convert(varchar(20),GETDATE() , 120))
i need the above displayed datetime in the format DD/MM/YYYY HH:MM:SS.
i have searched all over the place. Converting date format in T-SQL is available generally for datatype Varchar. Can someone advise me for datetime formatting via SQL directly?
thanks.
Either that, or you can specify the date/time format directly as the parameter of the ToString method:
string dateTime = DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss");
or second answer is
You can use any culture that supports the dd-mm-yyyy format like the french one.
For example to format the date time now in a format dd-mm-yyyy you can do as follows:
cultureInfo culture = new cultureinfo("fr-FR");
string oFormatedDate = dtNow.ToString("d", culture);