How to convert a datetime string to date and add +4 hours to it? - salesforce

in SFMC, I am trying to convert a string datetime format
2023-01-27T11:30:00.000+0000
Expected : 15:30 (adding 4 hours to time) and displaying only time.
I tried various functions like
DateParse: %%=DateParse(#dateStr)=%%
StringToDate: %%=StringToDate(#dateStr)=%%
but this doesn't work.
expecting to display only time by adding 4 hours to it.

%%[
SET #dateStr = "2023-01-27T11:30:00.000+0000"
SET #timeOnly = Format(DateAdd(#dateStr, 4, "H"), "HH:mm")
]%%
%%=v(#timeOnly)=%%
This will return "15:30"
The first line is setting the string to the variable #dateStr, so that this variable can be used in the next line of code.
The second line is using DateAdd() function with input of #dateStr, 4 as number of hours and "H" as the unit of hours. This will add 4 hours to the datetime.
Then, the Format() function is used with the DateAdd output as input, and "HH:mm" as format. This will format the datetime to only include the time in the format of hours and minutes. The output will be stored in the variable #timeOnly.
You can then use the variable #timeOnly to display it in your email or cloud page.

Related

Error scatter plot time vs wind speed (data from a table) MATLAB

I am trying to make a simple scatter plot in MATLAB with time on the x-axis and wind speed on the y-axis. I loaded in my data from a text file as a table and then tried to use table2array to plot since it needs numeric values not table data. I also tried using double and got another error.
Error Message: Error using scatter (line 55) Input arguments must be numeric or objects which can be converted to double.
Error in windconversions (line 18) scatter(time,wnd_TS)
I'm not sure if having the times as strings will also be an issue.
T = readtable('allunderway.txt', 'HeaderLines', 2);
%A = table2array(T)
date = T(:,1);
time = T(:,2);
wnd_TD = T(:,10);
wnd_TS = T(:,11);
table2array(wnd_TS);
table2array(time);
%double(wnd_TS);
scatter(time,wnd_TS)
A simpler way to access the data contained within the table is to use the dot notation, as T.VarN, where N is the number of the column you are interested in.
In your code you are using only 'time' for the plot, however this consists of hours, minutes and seconds only. I suspect that for your graphical analysis you require the combination of both the date and the hours.
It is possible to perform arithmetic addition on datetimes, however it is required that the two variables have the same format. By converting both dates to format 'MM/dd/yyyy HH:mm:SS' you are actually modifying the data of the variables. However, as stated in the documentation:
Since the data in the first column of the file ("date") have no time information, the time of the resulting datetime values default to midnight. Since the data in the second column of the file ("time") have no associated date, the date of the datetime values defaults to the current date.
When you add the variables date and time together together, you can add the date ('MM/dd/yyyy') of date to the time ('HH:mm:SS') of time.
An example of datetime conversion and addition follows.
Variables date and time before conversion:
date = 05/04/2011
time = 00:00:42
After conversion:
date = 05/04/2011 00:00:00
time = 06/01/2018 00:00:42
Adding the two:
05/04/2011 00:00:42
The code which reads the table and plots the scatter graph:
%Read table.
T = readtable('allunderway.txt', 'HeaderLines', 2);
%Access data of interest from table.
date = T.Var1;
time = T.Var2;
wnd_TS = T.Var11;
%Convert variable time to datetime.
time = datetime(time,'Format','HH:mm:SS');
%Add hours, minutes and seconds to variable date.
date = datetime(date,'Format','MM/dd/yyyy HH:mm:SS');
%Add month, day and year to variable time.
time = datetime(time,'Format','MM/dd/yyyy HH:mm:SS');
%Combine date and time variables.
fullt = date+timeofday(time);
scatter(fullt,wnd_TS);
The output of the code is the required scatter graph:
You can find more information on combining date and time from separate variables here.
Why do so much extra work. You could have simply used scatter(datenum(T.time),T.wnd_TS). That should do the job and save all the extra effort.

Date time not formating in AngularJs 1.6

I've got this format of date coming from an api:
"dateTime": "2018-02-19 00:00:00.000-08:00"
I just want the days, month and year in the format: dd/MM/yyyy
I'm using this in the view:
<td>{{dado.dateTime | date:'dd/MM/yyyy'}}</td>
But it's shows the date extacly as it comes from the API. Without any formatting
OBS: Angular 1.6
You are missing the T in your original DateTime. For ISO8601 DateTime it should be "2018-02-19T00:00:00.000-08:00" as the value. That is what can be translated by the angular date filter as a DateTime, currently it can only be interpreted as a string.
From the documentation on the input:
Date to format either as Date object, milliseconds (string or number) or various ISO 8601 datetime string formats (e.g. yyyy-MM-ddTHH:mm:ss.sssZ and its shorter versions like yyyy-MM-ddTHH:mmZ, yyyy-MM-dd or yyyyMMddTHHmmssZ). If no timezone is specified in the string input, the time is considered to be in the local timezone.
If you do not have any control over the server/source then you have a couple of options:
Change the value of the date time string in the factory/service when you receive the data so it is ISO8601 compliant. This can be as simple as using a regex or string concatenation as long as the T character is added between the date and time. In this specific case you could take the first 10 characters as you probably want to completely ignore the time portion but that would not work where you do want to show or keep time as a part of the datetime.
Create your own filter that then calls through to the built in date filter once you format the input correctly.

formatting date (saved in DB as datetime)

I have a bunch of records in SQL SERVER and i'm having an issue with one of the fields.
The datatype is datetime. The system was only inserting a date with no time in it, so '1/2/2017' - so when it was inserted in SQL SERVER it would show only as 1/2/2017 00:00:00. Now, what I'm trying to do is to display it just as it is saved in the DB.
I query the DB and display it like this...
If Not IsDBNull(dr("ReceivedOn")) Then
txtReceivedOn.Text = Format(dr("ReceivedOn"), "MM/dd/yyyy hh:mm tt")
End If
The mask on my masked textbox is liek this.....
00/00/0000 90:00 aa
When it does get displayd in the masked textbox is shows the date and 12:00 AM
1/2/2017 12:00:00 AM
Is there to get rid of it, and only show 0's instead of a incorrect time? However I'd like to only see a 12 hr time rather than 24.
You need to change the format specifier for hours from hh to HH
If Not IsDBNull(dr("ReceivedOn")) Then
txtReceivedOn.Text = Format(dr("ReceivedOn"), "MM/dd/yyyy HH:mm tt")
End If
On MSDN at the DateTime Custom Date and Time strings you can read
The "HH" custom format specifier (plus any number of additional "H"
specifiers) represents the hour as a number from 00 through 23; that
is, the hour is represented by a zero-based 24-hour clock that counts
the hours since midnight. A single-digit hour is formatted with a
leading zero.
Note that, with this format, the tt is meaningless.

Removing total days from Hhh:mm:ss

I have a varchar field that stores a duration for connections that occasionally goes into the days. I'm trying to separate the total number of days from the hh:mm:ss in this field so I can store them separately but I'm getting an out of range error whenever I try to convert this data to any type of datetime variation in order to datepart the days from the value and then deduct. Is their any way of doing this or will I need to resort to a crude charindex(':' just to remove the days?
You'll have to chop it up since it's not in a valid date or time format:
DECLARE #timeString VARCHAR(50) = '114:48:50'
SELECT Day_CT = LEFT(#timeString ,CHARINDEX(':',#timeString )-1)/24
,Tm = CAST(CAST(LEFT(#timeString ,CHARINDEX(':',#timeString )-1)%24 AS VARCHAR(12))+STUFF(#timeString ,1,CHARINDEX(':',#timeString )-1,'') AS TIME)
This is one of the many reasons why dates and times shouldn't be stored in string data types, it makes using them ugly.

How to concatenate year and month in tableau 8.0.0?

I have a datetime field .for e.g. 2013-08-22 12:00:00 AM. I want to concatenate the year and month and i want the output as 201308.
When i try year (datetime_field)+month(datetime_field) what i get get is 2013+08=2021 .. i.e it adds instead of conactenating. Can someone pl tell how to get the output as 201308?
something more like
str(year(datetime_field)) + str(month(datetime_field))
should give you what you want.
the str function converts your numeric data to a string, setting up the + to be a string-concatenation operation instead of an arithmetic-addition operation.

Resources