How to change my the date format in matlab? - matlab

I have
num4 = xlsread('dat.xlsx', 1, 'A:B');
dnum4=datetime(num4(:,1),1,1) + caldays(num4(:,2));
dnum4=
16-Jul-2008
18-Jul-2008
06-Aug-2008
08-Aug-2008
13-Aug-2008
15-Aug-2008
20-Aug-2008
22-Aug-2008
30-Oct-2008
I want to change the outputs from dd-mmm-yyyy to yyyy-mm-dd.
How to do that?

If you look at the documentation you'll see that datetime objects have a Format property that controls the display format:
>> t = datetime('now','TimeZone','local','Format','d-MMM-y HH:mm:ss Z')
t =
datetime
25-May-2017 10:26:46 -0400
>> t.Format = 'yyyy-MM-dd'
t =
datetime
2017-05-25

One way is to convert to datenum, then back to datestr:
newFmt = datestr(datenum(dnum4, 'dd-mmm-yyyy'), 'yyyy-mm-dd')

Related

boto 3 - loosing date format

I'm trying to read a parquet file using boto3. The original file has dates with the following format:
2016-12-07 23:00:00.000
And they are stored as timestamps.
My code in Sage Maker is:
boto_s3 = boto3.client('s3')
r = boto_s3.select_object_content(
Bucket='bucket_name',
Key='path/file.gz.parquet',
ExpressionType='SQL',
Expression=f"select fecha_instalacion,pais from s3object s ",
InputSerialization = {'Parquet': {}},
OutputSerialization = {'CSV': {}},
)
rl0 = list(r['Payload'])[0]
from io import StringIO
string_csv = rl0['Records']['Payload'].decode('ISO-8859-1')
csv = StringIO(string_csv)
pd.read_csv(csv, names=['fecha_instalacion', 'pais'])
But instead of the date I get:
fecha_instalacion pais
45352962065516692798029824 ESPAÃA
I loooked for dates with only one day in between and the nyuumber of digits that are the same are the first 6. As an example:
45337153205849123712294912--> 2016-12-09 23:00:00.000
45337116312360976293191680--> 2016-12-07 23:00:00.000
I would need to get the correct formated date, and avoid the especial characters.
Thanks.
The problem is the format. That Parquet file is using Int96 numbers to represent timestamp.
Here is a function to convert the int96Timestamp to python DateTime
import datetime
def dateFromInt96Timestamp(int96Timestamp):
julianCalendarDays = int96Timestamp >> 8*8
time = int((int96Timestamp & 0xFFFFFFFFFFFFFFFF) / 1_000)
linuxEpoch = 2_440_588
return datetime.datetime(1970, 1, 1) + datetime.timedelta(days=julianCalendarDays - linuxEpoch, microseconds=time)

how can i get the day of the week for a particular given date in Matlab

As today is Wednesday with date June 8, 2016. how can i write a code to get the day of given dates:
like what day is Nov 29
I'm trying to create a struct with
date
day
month
with month and date as input
Use the builtin weekday() function:
>> [num, name] = weekday('08-Jun-2016')
num =
4
name =
Wed
>> [num, name] = weekday('29-Nov-2016')
num =
3
name =
Tue
In addition to the weekday function, you can use the DDD or DDDD formats in the datestr function, like this:
datestr('08-Jun-2016','DDD') %Returns the string 'Wed'
datestr('08-Jun-2016','DDDD') %Returns the string 'Wednesday'
Or, to use a more practical format
datestr('08-Jun-2016','DDDD, mmmm DD, yyyy')
% Returns the string: 'Wednesday, June 08, 2016'

AppleScript countdown

I want to have a string that counts down from current time to alarm time.
I've manage to figer out how to get the current time and how to set the alarm time.
The problem I'm having is that when I take current time - alarm time it gives me a numer witch I then need to format back to a hh:mm:ss string.
i've got this.
set alarmHour to 23
set alarmMinute to 00
set theDate to the current date
set the hours of theDate to alarmHour
set the minutes of theDate to alarmMinute
set the seconds of theDate to 0
theDate
set countdown to theDate - (current date)
set ss to countdown / 60
at this point it gives me 22.283333333333 witch i now need to convert to hh:mm:ss and then put them into a sting that give me 00:22:00
UPDATE:
in swift you have % you can use
countDownTime = (formatterInteger - timeControlInteger)
let interval = Int(countDownTime)
let seconds = interval % 60
let minutes = (interval / 60) % 60
let hours = (interval / 3600)
but how to you do this in applescript?
Answer to second question:
is there a way to format strings like in swift? like
String(format:"%02d",absHour) – Mathias Halén
Yes, but you need to use the Satimage.osax scripting addition, available for free at:
Satimage AppleScript Additions
Satimage strftime() -- Date/Time Format Function
strftime v : format a date using a specification string like in the C
function strftime.
strftime date or list of date
into string : the formatting string. To obtain ISO 8601 dates, use
"%FT%TZ" or "%GW%V-%uT%TZ" (using the 'with GMT' parameter)
[GMT boolean] : if true, output date as GMT. Default: false, the ouput
date is local.
→ string : the formatted date
EXAMPLE: strftime (current date) into “%x” RETURNS: 07/22/14
"%a, %b %d, %Y" RETURNS: Tue, Jul 22, 2014
set d to current date
-- some ISO 8601 formats:
strftime d into "%FT%T%z"
-- "2007-01-15T16:10:56+0100"
strftime d into "%GW%V-%uT%T%z"
-- "2007W03-1T16:10:56+0100"
--if you need to store the date d as UTC:
strftime d into "%FT%TZ" with GMT
-- "2007-01-15T15:10:56Z"
strftime d into "%a, %b %d, %Y %H:%M:%S %z"
-- "Mon, Jan 15, 2007 16:10:56 +0100"

Date format issue with NSDateFormatter

I have this code:
var d = "22/12/1968 01:10:40"
var form : NSDateFormatter = NSDateFormatter()
form.dateFormat = "dd/MM/yyyy HH:mm:ss"
form.timeZone = NSTimeZone.localTimeZone() //Italy
print(form.dateFromString(d)!)
When I execute it I get this:
1968-12-22 00:10:40 +0000
first thing Why do I get "+0000" at end? and why the data format isn't respected?
Second , the time is wrong.
It's like the time is setted on London's time , but instead I setted it on my local time zone ( italy).
I tested it on iPhone Simulator
Why the date is wrong?
var d = "22/12/1968 01:10:40"
var form : NSDateFormatter = NSDateFormatter()
form.dateFormat = "dd/MM/yyyy HH:mm:ss"
//until here, there is no "problem"
form.timeZone = NSTimeZone.localTimeZone() //Italy //it means, the datetime is 22/12/1968 01:10:40 in Italy (GMT +1)
//Remember that NSDate is absolute time, or it holds GMT+0 time.
//As now, in Italy, it's 22/12/1968 01:10:40, then in GMT+0 time, it's 1h later. So, testDate holds 22/12/1968 00:10:40
NSDate testDate = form.dateFromString(d)!
Why it does not print the same format?
Because you did not use your NSDateFormatter object to get back date string but you used print() to print out directly the description of your object NSDate.

Matlab code to transfer between gregorian-hijri calendars

Is there a Matlab code that transfer the date ( day,month,year) from gregorian
to Hijri (Islamic) calendar and also from hijri to gregorian calendar,
Let's assume that we want to change the gregorian date:
Friday, 18 / 11 / 2011
to the Hijri date which is Friday 22 / 12 / 1432
Thanks
If you are on Windows, you could use the .NET Framework from inside MATLAB.
Here is a function to convert Gregorian dates to Hijri (based on an article on CodeProject):
function out = GregToHijri(str, frmtIn, frmtOut)
% English (US) and Arabic (Saudi Arabia) cultures
enCult = System.Globalization.CultureInfo('en-US',false);
enCult.DateTimeFormat.Calendar = System.Globalization.GregorianCalendar();
arCult = System.Globalization.CultureInfo('ar-SA',false);
arCult.DateTimeFormat.Calendar = System.Globalization.HijriCalendar();
% parse using supplied input format
dt = System.DateTime.ParseExact(str, frmtIn, enCult.DateTimeFormat);
% convert datetime as formatted string
out = char( dt.ToString(frmtOut, arCult.DateTimeFormat) );
end
Tested on your input:
>> GregToHijri('Friday, 18/11/2011', 'dddd, dd/MM/yyyy', 'dd/MM/yyyy')
ans =
22/12/1432