i am creating a web app in mvc-5 angularjs,
i need to show data of the following date entered by the user(in textboxes(i have two textbox)), but the user can only fetch the data of 1 month like, if i want to see data i can enter(01-01-2016) to (31-01-2016) or (29-01-2016) to (31-01-2016)
i a user enter the date like (31-01-2016) to (03-02-2016) then the alert will be shown with the error or if he enters the date like (01-01-2016) to (31-01-2017), then also the alert should appear,
my webservice will validate the data in following strings(frmdate and dateto)
what i need to do here?
What you need to do is to find the difference between the From date and To date and check whether the difference is 30 days or 31 days depending upon your logic.
Save those date strings as Date objects.
var fromDate = new Date($scope.fromDate);
var endDate = new Date($scope.toDate);
In order to use a Date object in any sort of calculation, we must first retrieve the Date's internal millisecond value, which is stored as a large integer since you can't just add or subtract Date objects together.
// Convert both dates to milliseconds
var fromDate_ms = fromDate.getTime();
var endDate_ms = endDate.getTime();
Then Calculate the difference in milliseconds
var difference_ms = endDate_ms - fromDate_ms;
To obtain the number of days for a given number of milliseconds, we would divide by 86,400,000, the number of milliseconds in a day (1000 x 60 seconds x 60 minutes x 24 hours):
var differene_days = Math.round(difference_ms/86400000);
If differene_days is equal to 30 or 31 show the report else show error message.
Related
The range A2:A contains months where each month uses the first day of the month. Users are allowed to input any day within that month when entering their data. The range of user input dates is defined by the named range "EAR_DATES_RNG". Each month has a corresponding summary of earnings for that month.
So here is an example of what this might look like:
User Input:
1/11/2017 - $100
2/9/2017 - $250
3/21/2017 - $500
Summary:
1/1/2017 - $100
2/1/2017 - $250
3/1/2017 - $500
Previously, I had a note forcing users to also use the first day of the month for their inputs, however I would like to free that up. However, this has caused my VLOOKUP() to stop working as it was previously using the summary date to find the corresponding user input:
=ARRAY_CONSTRAIN(ARRAYFORMULA(IFERROR(VLOOKUP(A2:A&" "&$A$1,{EAR_DATES_RNG&" "&EAR_NAMES_RNG,EAR_AMOUNTS_RNG},2,false),"")), IF(COUNT(A2:A)=0,1,COUNT(A2:A)),1)
How could I instead tell VLOOKUP to ignore the day of the month?
Or maybe there is a way I could somehow convert the user input dates to the first of the month using EOMONTH() tricks when I create the array in the formula, however maybe the fact that I use a named range will pose another problem for me to overcome.
I can however, see a solution already that would require a helper column that converts all the dates in EAR_DATES_RNG and then the formula creates the array using that converted range instead, but I would like to avoid a helper column if possible.
Appreciate any help I can get with this!
Screenshot examples:
OUTPUT:
[
INPUT:
January and March user inputs do not use the first day of the month, while February and April do. So as a result, in the 1st image, January and March values are blank in Column C, while February and April are showing up.
My VLOOKUP() is in Column C of the first image and is supposed to be grabbing the corresponding values of Column G in the second image.
try:
=ARRAY_CONSTRAIN(ARRAYFORMULA(IFERROR(VLOOKUP(TEXT(A2:A, "m/1/e")*1&" "&$A$1,
{EAR_DATES_RNG&" "&EAR_NAMES_RNG, EAR_AMOUNTS_RNG}, 2, 0))),
IF(COUNT(A2:A)=0, 1, COUNT(A2:A)), 1)
or:
=ARRAY_CONSTRAIN(ARRAYFORMULA(IFERROR(VLOOKUP(A2:A&" "&$A$1,
{TEXT(EAR_DATES_RNG, "m/1/e")*1&" "&EAR_NAMES_RNG, EAR_AMOUNTS_RNG}, 2, 0))),
IF(COUNT(A2:A)=0, 1, COUNT(A2:A)), 1)
Here is a script version of the function you want.
function firstOfTheMonth() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//Gets the values of column A until the last row with data
var range = sheet.getRange(2,1, sheet.getLastRow() - 1, 1);
//Changes the date values of column A into MM-YYYY-1
// You need to define your TimeZone on Utilities.formatDate(new Date(y), "YOUR TIMEZONE HERE", "MM-1-yyyy")
var val = range.getValues();
var newval = val.map(x => x.map(y => Utilities.formatDate(new Date(y), "GMT+8", "MM-1-yyyy")));
console.log(newval);
range.setValues(newval);
}
You can manually add the function on an onEdit Trigger so that the function will automatically run whenever an edit is made on the spreadsheet.
Before:
After the script was ran:
Reference:
https://developers.google.com/apps-script/reference/utilities/utilities#formatDate(Date,String,String)
I'm currently implementing moment in my React project to format dates of schedules set up in the database for sending notifications. My system has a dashboard displaying all notifications scheduled to go out and their respective dates. The database currently has two columns for recording the scheduled date and time - schedule_date and schedule_time - and currently I display them on the dashboard using the following code:
{moment(file.schedule.schedule_date).format('DD/MM/YYYY')}
{moment(file.schedule.schedule_time, 'HH:mm:ss').format('h:mma')}
schedule_date has the format YYYY-MM-DD and schedule_time has the format HH:MM:SS (pseudo) However, I need now to be able to compare this date with the current date in order to determine whether or not the set schedule for a notification has passed. I used the following code to get today's date and time:
let now = moment();
But I am having difficulty being able to compare this against the actual date / time in the database. I don't want to just compare against the date because if the notification was due to go out at 9.00am and it's now 10.00am it would still assume it hadn't been sent if it was only compared against date.
Is there a way to do this comparison in the front end?
You can set the time to your date's moment object this way :
var date = '2018-09-12T00:00:00Z';
var time = '16:00:00';
var momentDate = moment(date);
var momentTime = moment(time, 'HH:mm:ss');
momentDate = momentDate.set({
hour: momentTime.get('hour'),
minute: momentTime.get('minute'),
second: momentTime.get('second')
});
console.log(momentDate.format());
console.log(momentDate.isAfter(moment.now()));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>
You can also try to create the iso date format from your date and time strings directly like this :
var fullDate = date + 'T' + time + 'Z';
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.
I'm creating a drill down report using a column chart to display data for a given day. Each column shows the max value in that hour of the day. When I click a column the same query is run in a line chart but the date range is changed to be for only that hour of data, passing through the field value of testDate as a parameter fromDate the date range looks like this: testDate >= fromDate AND testDate < DATEADD(HOUR, 1, fromDate). So only that hour of data is got.
My problem is that not all values within the hour are always displayed, most reports when clicked into show the whole hour: Whole hour but some of them show only the first 35 minutes and cut off before an expected very large number: Cut off data In this case a value of 948 should occur at around 7:47 but just doesn't appear.
Any help would be greatly appreciated.
EDIT:
I bumped it up to a 2 hour window which showed all of the data and it will do the job, but I'm still curious as to why it wasn't working before, here is an image of data from 07:00 to 09:00, Two hour window
When I send a form on my web, the insert query saves the current date on my DB using the function now().
Now, I'm trying to get this column in minute format to calculate other thinks that I need, but I don't know how to do that.
For example, I have this:
"2013-05-08 08:30:00"
And I want this (now 8.50):
"20" <- In minutes
Thanks
OK, let's suppose you have a table with a timestamp:
CREATE TABLE ex (t timestamp);
INSERT INTO ex VALUES ('2013-05-08 8:30'::timestamp);
And you want the difference in minutes between the column t and now(). You can get that using the extract function:
SELECT extract(epoch from (now() - ex.t)) / 60 FROM ex;
epoch is the number of seconds from the "epoch" for date and timestamp types, but is't just the number of seconds in the interval for interval types. By dividing it by 60 you get what you want (if you want an integer number of minutes just trunc it.)