Compare date to current month year - angularjs

In one of the API I am using I have a date in the format yyyy-MM-dd hh:mm:ss
I am trying compare this date to just the current month and year. How do I just take out the month and year from that date and compare it in AngularJS?
I read the AngularJS Documentation for date formats and I can't seem just get the month and year from that format so that I can compare it with the current month and year

Refer momentjs docs.
Docs to parse date
Docs to get from date
Refer below code snippet,
var apidate = moment('2019-01-01 11:31:23', 'YYYY-MM-DD hh:mm:ss'); // API date
console.log("Day="+apidate.date()+ " Year="+apidate.year());
var now = moment(); // current date
console.log("Day="+now.date()+ " Year="+now.year());
// compare day and year of both dates
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

Using momentjs.com
Convert the response from your API with eg;
responseDate = moment(responseAPI).format('YYYYMM')
You can then you can compare it with an existing date eg;
if (responseDate != moment(existingDate).format('YYYYMM')) { }

Related

Set report date for date other than today in SQL reporting

I have a report that I have been asked to produce. The first column of data is total time entered from the first of the year to the end of the previous month. The next column of data is total time from beginning of "current" month to the end of "current" month.
For example. If this report was being run for March then the first column would be total time for Jan and Feb and the second column would be total time for March. If I were to run it in April then the first column would be total time for Jan/Feb/Mar and the second column total time for April etc.
I am using various expressions to get first date of the year, last date of previous month, first date of this month, last date of this month. All working fine and it runs like a dream if you run the report in the current month (i.e. March) but if you want to run the report in April for March's data it won't do it as it's reading the date on the computer and using that to calculate the prev month.
In Crystal reports you can set a report date. Is there something similar in SQL reporting? I'm assuming you DECLARE the report date in your initial query but I haven't yet found the right combination of functions.
This report is for an external bit of software that we run.
The only parameters I can use are #FromDate and #ToDate and these are set as text rather than date
I was planning on using the #ToDate to set the report date but would obviously have to convert it from text first
Any guidance very much appreciated
Try this. You could actually do it without all the declare statements, I just did those to break it down to be easy to read.
declare #date_entered datetime = '2/7/2017'
declare #start_of_year datetime = DATEADD(yy, DATEDIFF(yy, 0, #date_entered), 0)
declare #start_of_month datetime = DATEADD(month, DATEDIFF(month, 0, #date_entered), 0)
declare #end_of_month datetime = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,#date_entered)+1,0))
declare #end_of_last_month datetime = DATEADD(day,-1,#start_of_month)
-- Now remove weekends from days found
declare #amount_of_days_previous tinyint = datediff(day,#start_of_year,#end_of_last_month) - (datediff(wk, #start_of_year, #end_of_last_month) * 2)
declare #amount_of_days_this tinyint = DATEDIFF(day,#start_of_month,#end_of_month) - (datediff(wk, #start_of_month, #end_of_month) * 2)
select #amount_of_days_previous * 8 as WorkHoursinDaysPrevious,
#amount_of_days_this * 8 as WorkHoursinDaysThis
I had a think over the weekend and realised that I was approaching the problem all wrong. I was trying to set the report date so that my query could read that date and then go back and find the relevant date (first date in the year, last date of previous month etc).
What I needed to do was use my date criteria (FromDate and ToDate) in my DATEADD selections.
CONVERT(datetime,#FromDate,103) AS FIRSTDAYOFYEAR,
DATEADD(D, - 1, DATEADD(MONTH, DATEDIFF(MONTH, '19000101', CONVERT(datetime,#ToDate,103)), '19000101')) AS LASTDAYPREVMONTH,
DATEADD(MONTH,DATEDIFF(MONTH, '19000101',CONVERT(datetime,#ToDate,103)), '19000101') AS FIRSTDAYMONTH,
CONVERT(datetime,#ToDate,103) AS LASTDAYMONTH,
This is now much better because my "first date of year" can be anything the user wants, not just the first day of the year.

How to create query with format date DD-MM-YYYY

I have query database like this:
SELECT * FROM TABLE WHERE start_date = '01-10-2016' //DD-MM-YYYY
But above code error and actually in database date format is YYYY-MM-DD.
So how to create query with format date DD-MM-YYYY?
A proper date column (data type date !) has no format. Then it's enough to get your data input for a date column right, use to_date() for non-standard input format like #Shiva posted. Or better yet, always provide date literals in ISO 8601 format 'YYYY-MM-DD' to begin with, which works with any locale setting.
If you are running a broken design with dates stored as text, then combine to_date() and to_char() to transform any valid date format into any other text format:
SELECT * FROM tbl
WHERE start_date = to_char(to_date('01-10-2016', 'DD-MM-YYYY'), 'YYYY-MM-DD');
May be you can use to_date function to convert the above format into the standard format.
For instance,
SELECT to_date('01-10-2016', 'DD-MM-YYYY');
----------
2016-10-01
1 row
https://www.techonthenet.com/postgresql/functions/to_date.php

How to set date to datetime format or timestamp in calendar Angular JS?

I use the following Angular JS calendar.
When I select date I get date format:
calendarDay: Sat Oct 10 2015 20:07:14 GMT+0500 (Грузинское время (лето))
How I can save this date in format timestamp or datetime for MySql in hidden input field?
You can call the getTime method of the date:
<input type="hidden" value="{{calendarDay.getTime()}}">
Tell me if it works for you.

How do I use dateadd to get the first day of last year?

I'm trying to pull data that falls in between January 1st, 2014 and today's date last year (ie. August 3rd, 2014). How would I go about using ``dateadd` to get the date 1/1/14? I'm using the following code to get the date 8/3/14.
dateadd (yy, -1, getdate())
I want to avoid explicitly searching for 1/1/14 because in a year's time I'd like the sql query to find 1/1/15 without me having to go back in and rewrite it.
Use DATEFROMPARTS:
DATEFROMPARTS(YEAR(GETDATE()) - 1, 1, 1)
DATEADD(yy, DATEDIFF(yy,0,getdate())-1, 0)
DATEFROMPARTS() is the best way but it requires SQL2012 or later. If you're on an earlier vierion, try this:
You can use GETDATE() to get the current date
You can use the function YEAR() to extract the year from any date
Subtract 1 from it to get last year
Append 1/1/ to the front of it
Convert it back to a date again
select convert(datetime, '1/1/' + convert(varchar(max),year(getdate())-1))

Calculate a Future Date in Memory in WinForms

I have a Date in DateTimePicker I want to add say x no of days and then compare it with todays date in an if statement
Please Help me in Calculating the Date i.e how to add no of days to a Date
Please Help,Thanks in Advance
Use the AddDays method
Assuming you know how to get the DateTime from the datePicker,
DateTime dateFromPicker = DateTime.Now;
DateTime tomorrow = dateFromPicker.AddDays(1);
Console.WriteLine(tomorrow.ToString());

Resources