how to do Date Comparision in javascript? - javascript-events

I need to create the javascript Function based on Date Comparison.which means,To compare the Currentday with Currentday+7.
function a(value)
{
//here how to get current date
//here how to add 7 days with current date
return addedvalue;
}
function b(value)
{
var s=a(value);
//again getting current date here
//check this function current date with s.then if its true means show,
alert('next 7th day is'+s);
}
How to do this?

var now = new Date();// gives you the current date.
var sevenDaysFromNow = new Date();
sevenDaysFromNow.setDate(now.getDate() + 7); // adds seven days.
function compareDates(d, dd) {
return d - dd;
};
Date comparison
If you have two date objects, in this case now and sevenDaysFromNow, subtracting them now - sevenDaysFromNow will return the difference in milliseconds between the two dates. You can convert the difference to a different time unit using simple maths.
var ms = sevenDaysFromNow - now;
var hoursDiff = ms / 1000 / 60 / 60;
var daysDif = ms / 1000 / 60 / 60 / 24;
Working fiddle

This will give you the current date:
new Date()
This will transform it into milliseconds since EPOCH:
(new Date()).valueOf()
This will calculate how much time 7 days are:
7 * 24 * 60 * 60 * 1000
This will transform your integer back to a date:
new Date(any number of millisecond since EPOCH);
To complete the example:
function a() {
return new Date((new Date()).valueOf() + (7 * 60 * 60 * 24 * 1000));
}

Date comparison is done using the DateDiff function.
We dont have any comparison function for Date

Related

Ruby: how to convert VARIANT DATE to datetime

In my project i get from external system date&time in VARIANT DATE type and need to convert it to datetime (i.e. 43347.6625 => 04/09/2018 16:29:59).
Do you know how to do it in ruby? what is the best approach? I did not find any ruby built-in method to do such a conversion...
here a method to do the calculation, the date you give is not correct, it should be what this method is returning, check with https://planetcalc.com/7027/
def variant2datetime variant
# number of days after 1-1-1900 minus 2 days for starting with 0
# and having a day that didn't exist because 1900 wasn't a leap year
date = Time.new("1900-01-01") + (variant.to_i - 2) * 24 * 60 * 60
fraction = variant % 1
hours = (fraction - fraction.to_i) * 24
minutes = (hours - hours.to_i) * 60
seconds = (minutes - minutes.to_i) * 60
Time.new(date.year, date.month, date.day, hours.to_i, minutes.to_i, seconds.to_i)
end
variant2datetime 43347.6625 # 2018-09-04 15:53:59 +0200

Ruby - Integer to Time conversion for specific format (y m d)

given
today = 20150307
and
t= Time.at(today).strftime("%Y%m%d")
why this does not return
20150307
but instead
19700822
I ma triyng to check if the difference of thwo date is more than 7 days but those two values are converted into integer in the first place
example
a = 20150227 #(25th February 2015)
x = 20150307 #(7tharch 2015)
if (x-a > 7)
puts "This Item is overdue"
else
puts "All good"
end
my original today is given by this
today = Time.now.strftime("%Y%m%d").to_i
oneweek = (Time.now + (60 * 60 * 24 * 7)).strftime("%Y%m%d").to_i
if i do oneweek - today it will be an integer difference not a date one...
how can i achieve this???
Time.at spect the number of seconds from 1970-01-01 (Epoch).
To do what you want, try something like: t = Date.strptime("20150307", "%Y%m%d")

Local system based timestamp on 00:00:00 of particular day

How do i get time in seconds from epoch at 00:00:00 of a particular day on local machine.I want to trigger a service on 00:00:00 of a particular day and so i wanted to know time in seconds from epoch on local machine when system time reach to 00:00:00.
I have to do it using C language .
Any help will be appreciated .
That's what the mktime function is for. See the documentation here.
struct tm time_str;
time_str.tm_year = 2014 - 1900; /* year minus 1900 */
time_str.tm_mon = 8 - 1; /* month minus 1 */
time_str.tm_mday = 11;
time_str.tm_hour = 0;
time_str.tm_min = 0;
time_str.tm_sec = 0;
time_str.tm_isdst = -1;
time_t seconds_since_epoch = mktime(&time_str);

Compute the number of seconds to a specific time in a specific Time Zone

I want to trigger a notification for all my users at a specific time in their time zone. I want to compute the delay the server should wait before firing the notification. I can compute the time at the users Time Zone using Time.now.in_time_zone(person.time_zone)
I can strip out the hours, minutes and seconds from that time and find out the seconds remaining to the specific time. However, I was wondering if there's a more elegant method where I could set 9:00 AM on today and tomorrow in a timezone and compare it with Time.now.in_time_zone(person.time_zone) and just find out the number of seconds using arithmetic operations in the ruby Time Class.
Or in short my question is: (was: before the downvote!)
How do I compute the number of seconds to the next 9:00 AM in New York?
What about this
next9am = Time.now.in_time_zone(person.time_zone).seconds_until_end_of_day + 3600 * 9
next9am -= 24 * 60 * 60 if Time.now.in_time_zone(person.time_zone).hour < 9
NOTIFICATION_HOUR = 9
local_time = Time.now.in_time_zone(person.time_zone)
desired_time = local_time.hour >= NOTIFICATION_HOUR ? local_time + 1.day : local_time
desired_time = Time.new(desired_time.year, desired_time.month, desired_time.day, NOTIFICATION_HOUR, 0, 0, desired_time.utc_offset)
return desired_time - local_time

Linq Query - get current month plus previous months

I need to build a Linq query that will show the results as follow:
Data:
Sales Month
----------------------
10 January
20 February
30 March
40 April
50 May
60 June
70 July
80 August
90 September
100 October
110 November
120 December
I need to get the results based on this scenario:
month x = month x + previous month
that will result in:
Sales Month
--------------------
10 January
30 February (30 = February 20 + January 10)
60 March (60 = March 30 + February 30)
100 April (100 = April 40 + March 60)
.........
Any help how to build this query ?
Thanks a lot!
Since you wanted it in LINQ...
void Main()
{
List<SaleCount> sales = new List<SaleCount>() {
new SaleCount() { Sales = 10, Month = 1 },
new SaleCount() { Sales = 20, Month = 2 },
new SaleCount() { Sales = 30, Month = 3 },
new SaleCount() { Sales = 40, Month = 4 },
...
};
var query = sales.Select ((s, i) => new
{
CurrentMonth = s.Month,
CurrentAndPreviousSales = s.Sales + sales.Take(i).Sum(sa => sa.Sales)
});
}
public class SaleCount
{
public int Sales { get; set; }
public int Month { get; set; }
}
...but in my opinion, this is a case where coming up with some fancy LINQ isn't going to be as clear as just writing out the code that the LINQ query is going to generate. This also doesn't scale. For example, including multiple years worth of data gets even more hairy when it wouldn't have to if it was just written out the "old fashioned way".
If you don't want add up all of the previous sales for each month, you will have to keep track of the total sales somehow. The Aggregate function works okay for this because we can build a list and use its last element as the current total for calculating the next element.
var sales = Enumerable.Range(1,12).Select(x => x * 10).ToList();
var sums = sales.Aggregate(new List<int>(), (list, sale) => list.Concat(new List<int>{list.LastOrDefault() + sale});

Resources