Set a date based on other dates? - apache-flex

I need to set a chart to start at midnight of the current day and end just before midnight of the next day...
I'm trying to do something like this:
minChartDate = currentDate.fullYear,currentDate.month,currentDate.date,0,0,0,0;
where currentDate:Date; is the day currently selected.
I'm getting an implicit coercion error between the Number to Date type, as if currentDate.fullYear is a Date, but according to the documentation it should be a number. Or is my syntax defining this incorrect? Also wondering if there's a simpler way to get the min and max dates than this!
(the reason I am setting this is so that it starts at midnight rather than at the first data point in the series).
I'm also getting a weird error 'maximum' values of type Date cannot be represented in text.. it said I need a Date type object for the minimum and maximum so I'm really not sure what it's talking about...

this code will make a date object set to 0:00 on today.
var minChartdate:Date= new Date();
minChartdate.hours=0;
minChartdate.minutes=0;
minChartdate.seconds=0;
minChartdate.milliseconds=0;
trace(minChartdate)
To make one for the next day:
var minChartdate:Date= new Date();
minChartdate.time = minChartdate.time+1000*60*60*24 // one day in milliseconds
minChartdate.hours=0;
minChartdate.minutes=0;
minChartdate.seconds=0;
minChartdate.milliseconds=0;
trace(minChartdate);
This script moves the date object forward by 24 hours, and then sets hours, mins,sec,milisecs to 0.
Note: This is not 100% correct solution, it can fail on days, when the hours are adjusted because of daylight saving time changes.

Related

Spotfire change datetime column to monday of weekday

im using spotfire software and i have a datetime column like:
DateTime
21/07/2022 12:11:01
21/07/2022 14:32:01
04/12/2022 10:22:01
30/06/2022 16:22:01
how can i created a new calculated column where it instead changes the date to the monday of the week for all values?
many thanks
i can do this in power bi by dropping the time and using the function
Start of Week Monday = 'Data'[DateTime ]+1-WEEKDAY('Data'[DateTime ]-1)
this results in all the values changing to the monday of its given week/month/year
how can i do this in spotfire?
Assuming you have the following functions available to you: DateAdd, DayOfWeek (*)
You could try:
DateAdd("dd",1 - (If(DayOfWeek([original_Date])=0,7,DayOfWeek([original_Date]))),[original_Date])
This resets the date (including the time portion) to the previous Monday.
Initially I had tried the simpler formula:
DateAdd("dd",1 - DayOfWeek([original_Date]),[original_Date])
but Sunday is mapped to a zero, so whenever the original day was a Sunday, it was pushed to the following Monday.
It does depend on your original settings so you may have to play around with the numbers a bit.
(*) depending on what the source of your data is, equivalent functions may be available.

How to add two hh:mm:ss format and get hh:mm:ss

I have below format of date and need a beloe o.p in momen
var myDate="2019-01-26";
console.log(moment(myDate,'YYYY-MM-DD 00:00:00').add("2019-01-26 04:00");
expected o/p be 2019-01-26 04:00:00
Based on this, and your previous similar questions it seems that you are trying to add a specific amount of time to a moment-defined date. Perhaps the solution is simpler than you expect. The "add" method takes two arguments; a number representing the quantity to be added, and the type of unit (weeks, days, hours, etc.)
var myDate="2019-01-26";
console.log(moment(myDate,'YYYY-MM-DD 00:00:00').add(4, "hours");
seems to return the value you are looking for. the momentjs documentation is excellent. https://momentjs.com/docs/

Moment js diff with different timezone

I want to calculate the difference of time in hours between 2 different time zones using the moment.js library.
I tried using diff function provided by the library but it is not providing correct answers, I tried:
moment('2016-12-18 6:00:00', 'YYYY-MM-DD HH:mm:ss')
.diff(moment('2016-12-18 6:00:00', 'YYYY-MM-DD HH:mm:ss').utcOffset('+09:30'), 'hours')
I am in UTC+5:30 and the moment(string, string) constructor returns the time in my current timezone. And I was expecting the answer to be 4 instead got 0. Is there any other function that moment library provides to get difference amongst timezones ?
As long as you are just using fixed time zone offsets, your above code will work if you make one adjustment:
.utcOffset('+09:30', true)
The second parameter tells the function to retain the date and time value the moment already has when the offset is set. The default (false) will shift the date and time from its current offset (your local time) to the offset provided.
If you are not working with fixed offsets, then you will indeed need to use the moment-timezone add-on.

A difference between startof and firstof values of indexAt parameter in to.period

According to the documentation:
To adjust the final indexing style, it is possible to set indexAt to
one of the following: ‘yearmon’, ‘yearqtr’, ‘firstof’, ‘lastof’,
‘startof’, or ‘endof’. The final index will then be yearmon, yearqtr,
the first time of the period, the last time of the period, the
starting time in the data for that period, or the ending time in the
data for that period, respectively.
Now I try to do to.hourly for my minute data. I see that all values by default is set to the last minute in data. I want to set to the first minute. It seems indexAt is parameter exactly for that. I have one hour that starts at 09:30. As I understand from description 'firstof' should set it to 09:00 (first minute of hourly period) and 'startof' should set it to 09:30 (first available minute in hour data). 'startof' seems to work for me but 'firstof' does not work and still returns 09:59! Am I missing something?
If you look at the Usage section of ?to.period, you will see that only to.monthly and to.quarterly have indexAt arguments. That is why to.hourly ignores the indexAt argument.
Issue #158 briefly discusses the possibility of adding indexAt support for periods other than monthly and quarterly.

timedeltas and datetimes subtraction and converting to duration in minutes

I am at a standstill with this problem. I outlined it in another question ( Creating data histograms/visualizations using ipython and filtering out some values ) which meandered a bit so I'd like to fix the question and give it more context since I am sure others must have a workaround for this or have the problem. I've also seen similar, not identical, questions asked and can't quite adapt any of the solutions thus far given.
I have columns in my data frame for Start Time and End Time and created a 'Duration' column for time lapsed. I'm using ipython.
The Start Time/End Time columns have fields that look like:
2014/03/30 15:45
A date and then a time in hh:mm
when I type:
pd.to_datetime('End Time') and
pd.to_datetime('Start Time')
I get fields resulting that look like:
2014-03-30 15:45:00
same date but with hyphens and same time but with :00 seconds appended
I then decided to create a new column for the difference between the End and Start times. The 'Duration' or time lapsed column was created by typing in one command:
df['Duration'] = pd.to_datetime(df['End Time'])-pd.to_datetime(df['Start Time'])
The format of the fields in the duration column is:
01:14:00
no date just a time lapsed in the format hh:mm:ss
to indicate time lapsed or 74 mins in the above example.
When I type:
df.Duration.dtype
dtype('m8[ns]') is returned, whereas, when I type
df.Duration.head(4)
0 00:14:00
1 00:16:00
2 00:03:00
3 00:09:00
Name: Duration, dtype: timedelta64[ns]
is returned which seems to indicate a different dtype for Duration.
How can I convert the format I have in the Duration column to a single integer value of minutes (time lapsed)? I see no methods that I can use, I'd write a function but wouldn't know how to treat the input of hh:mm:ss. This must be a common requirement of data analysis, should I be going about converting these dates and times differently if my end goal is to get a single integer indicating minutes lapsed? Should I just be using Excel?... because I have so far spent a day on this problem and it should be a simple problem to solve.
**update:
THANK YOU!! (Jeff and Dataswede) I added a column with the command:
df['Durationendminusstart'] = pd.to_timedelta(df.Duration,unit='ns').astype('timedelta64[m]')
which seems to give me the Duration (minutes lapsed) as wanted so that huge part is solved!
What still is not clear is why there were two different dtypes for the same column depending how I asked, oh well right now it doesn't matter.**

Resources