QUICK

QUICK

WRITE A PROGRAM IN YOUR FAVORITE LANGUAGE THAT CALCULATES THE DIFFERENCE IN DAYS BETWEEN TWO TIMESTAMPS

OR THIS BIRD IS GONNA STAB YOU

#include
#include

typedef struct {
int year; /* 0 to 9999 */
int month; /* 1 to 12 */
int day; /* 1 to 31 */
} date;

int leapyear(int year) {
if(year % 400 == 0) {
return 1;
}
if(year % 100 == 0) {
return 0;
}
if(year % 4 == 0) {
return 1;
}
return 0;
}

int dpm(int year, int month) {
switch(month) {
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
return 28 + leapyear(year);
default:
return 31;
}
}

int passed(date* d) {
int days = d->day - 1;
int month;
for(month = 1; month < d->month; month++) {
days += dpm(d->year, month);
}
return days;
}

int timespan(date* from, date* to) {
int days = passed(to) - passed(from);
int year;
for(year = from->year; year < to->year; year++) {
days += 365 + leapyear(year);
}
return days;
}

int main(void) {
date a = {1970, 1, 1};
date b = {2016, 7, 13};
int days = timespan(&a, &b);
printf("%d\n", days);
return EXIT_SUCCESS;
}

inb4 homework

wow how did you do that in 53 seconds

I'm the OP you fucking moron. Now get to work or the bird will cut you a new asshole.

This thread has been posted every day for a while now..

return t2 - t1; // return Unix timestamp difference

LOL

10 PRINT "404"
20 GOTO 10

that bird looks like he means it...
damn...

wow i just

wow

def difference(a, b):
return (datetime.fromtimestamp(b) - datetime.fromtimestamp(a)).days

you just wow, learning some basic grammar you spastic

public class DayDiff {
public static void main(String[] args){
DateTime dayOne = DateTime.Now;
DateTime dayTwo = new DateTime(2016, 2, 16);
var diff = dayOne - dayTwo;
Console.WriteLine(diff);
}
}

import "time"

func dayDiff(a, b time.Time) float64 {
return float64(b.Sub(a)) / float64(24 * time.Hour)
}

well it's a different exercise each time so it's alright

oops, better version

import "time"

func dayDiff(a, b time.Time) float64 {
return b.Sub(a).Hours() / 24
}

Wrong. Enjoy your stab.

=A2 - A1


make sure to format the cell as a number

...

Bring back containment board /prog/

programming on Sup Forums is dropouts who think they're better than "consumer threads" who are just as unemployable as they make the "kids" out to be

this isn't a challenge it just ends up in autism of dropouts pointing out the most autistic irrelevant shit like having an empty line with a { or not or using // over */ literally nobody besides dropout first years even THINK about this shit

you guys are like that little group of smelly hippies outside every 101 class, signed up then skipped the whole class just to jack each other off about how their obscure hipster 70s ironic stancefag-tier butt diddling lang is superior because it's shitty and unusable

Bring back containment board /prog/

static int MyLifeIsShitKillMeAnyway(DateTime a, DateTime b)
{
return (b - a).Days();
}

That's not a language, dumb wageslave

what fucking language is that?

fuck off retard

shit = fuck1-fuck2
asspiss = shit % 86400
print asspiss

wait it should be / not % i'm dum

Lisp

is it better than java?

nigga are you bein serious?

sorry we don't all have the time to pick up random languages just because we feel like it

function days_between(date1, date2) {

// The number of milliseconds in one day
var ONE_DAY = 1000 * 60 * 60 * 24

// Convert both dates to milliseconds
var date1_ms = date1.getTime()
var date2_ms = date2.getTime()

// Calculate the difference in milliseconds
var difference_ms = Math.abs(date1_ms - date2_ms)

// Convert back to days and return
return Math.round(difference_ms/ONE_DAY)

}

#!/usr/bin/env python
import argparse
import sys
from datetime import datetime

parser = argparse.ArgumentParser(description=
'Calculates difference in days between two dates\n'
'Default date format: DD.MM.YYYY')
parser.add_argument('a', help='Date a')
parser.add_argument('b', help='Date b')
parser.add_argument('--murrica', help='Switches to the murrican date format (MM/DD/YYYY)', action='store_true')
parser.add_argument('--timestamp', help='Dates are provided as a timestamp', action='store_true')
args = parser.parse_args()

datefmt = '%d.%m.%Y'
if args.murrica:
datefmt = '%m/%d/%Y'

try:
if args.timestamp:
date_a = datetime.fromtimestamp(float(args.a))
date_b = datetime.fromtimestamp(float(args.b))
else:
date_a = datetime.strptime(args.a, datefmt)
date_b = datetime.strptime(args.b, datefmt)

except:
print 'Invalid arguments you dipshit\n'
parser.print_help()
sys.exit(1)

delta = date_b - date_a
if abs(delta.days) == 1:
print 'The time difference is %d day' % delta.days
else:
print 'The time difference is %d days' % delta.days

C and Lisp aren't "random languages" you dolt.

well, Lisp is.

itt: we write stuxnet one line at a time

lisp's influence is second only to C

lol whatever grandpa

Nice!

you can get pretty far as a stupid programmer

yeah

in 1960 maybe

fixing your last print to python3 too:

print "The time difference is {} day{}".format(delta.days, "" if abs(delta.day) == 1 else "s")

sorry, python2 only, not using print()

webdev is pretty big

yep and everybody is using lisp on the net cool

...

$.getScript("timestamps.js");
calculateDifference(a, b);


get on my level

i'm implying web developers are dumb, stupid

Everyone in this thread relying on unix epoch time stamps is getting stabbed.

oh okay

nice to see lisp is as relevant as ever

Let's see your LISP solution then, smartass.

Like I said, I dont waste my time

BIRDMIN NO

Trips kill bird permanently. Check em.

Leap seconds don't exist in epoch time. A day will always be 86400 seconds long. This problem can be solved using only timestamps and arithmetic.

Nobody cares about leap seconds. The issue is with unix time stamps starting at 1970.

pretty sure the old style formatting still works in python3

parentheses are needed in python3 though, which you're missing

the problem as stated is ill-defined.

was the bird expecting this program to work on inputs more than 292,471,208,677 years before or after 1970?

>JavaScript
/code

function stampDifferenceInDays(a,b){
return Array.from(arguments).reduce((a,b) => Math.abs(a-b)) * 1000 / 60 / 60 / 24;
}

/code

function stampDifferenceInDays(a,b){
return Array.from(arguments).reduce((a,b) => Math.abs(a-b)) * 1000 / 60 / 60 / 24;
}

Considering that dealing with time is not deterministic I suppose the bird will let you get away with an almost-correct answer too.

Lol calm down

go fuck yourself , and tell your mother I said hi .

-[------->+--.[--->+++.[--->+-----.++[->+++.-[--->+--.+[->++++.++++++++.-[++>---+.--[->+++++.----------.++++++.---.+.++++[->+++.+++++++.------.-[->+++-.

I really don't know why I took out an hour out of my life to do this.
GLOBAL _start
EXTERN printf

STRUC DATE
.Year: RESD 1
.Month: RESD 1
.Day: RESD 1
ENDSTRUC

SECTION .data
Result: DB "There are %d days between the two dates", `\n`, `\0`
Days: DD 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
Date1: ISTRUC DATE
AT DATE.Year, DD 1990
AT DATE.Month, DD 1
AT DATE.Day, DD 3
IEND
Date2: ISTRUC DATE
AT DATE.Year, DD 2016
AT DATE.Month, DD 7
AT DATE.Day, DD 13
IEND
SECTION .text

%MACRO MOD_EQZERO 2
mov eax, %1
mov ebx, %2
cdq
idiv ebx
test edx, edx
%ENDMACRO

%MACRO IS_LEAPYEAR 1
MOD_EQZERO %1, 4
setz al
jnz %%done
MOD_EQZERO %1, 100
setnz al
jnz %%done
MOD_EQZERO %1, 400
setz al
jnz %%done
mov al, 1
%%done:
movsx eax, al
%ENDMACRO

_start:
IS_LEAPYEAR [Date2+DATE.Year]
add [Days+8], eax
xor ecx, ecx
mov edi, [Date2+DATE.Day]
sub edi, 1
get_days_to:
add edi, [Days+4*ecx]
add ecx, 1
cmp ecx, [Date2+DATE.Month]
jl get_days_to

mov Dword [Days+8], 28
IS_LEAPYEAR [Date1+DATE.Year]
add [Days+8], eax
xor ecx, ecx
mov esi, [Date1+DATE.Day]
sub esi, 1
get_days_from:
add esi, [Days+4*ecx]
add ecx, 1
cmp ecx, [Date1+DATE.Month]
jl get_days_from

sub edi, esi
mov ecx, [Date1+DATE.Year]
get_years:
add edi, 365
IS_LEAPYEAR ecx
add edi, eax
add ecx, 1
cmp ecx, [Date2+DATE.Year]
jl get_years

mov rsi, rdi
mov rdi, Result
call printf

mov eax, 60
mov ebx, 0
syscall

oh.

I really like the stabbing birds threads. Some solutions are really creative. Also, it's cool to get a look at other languages beyond Hello World.

No one uses Lisp outside of academia

Try this again and see if I can do it right.
Module Module1
Sub Main()
Dim i As DateTime
Dim o As DateTime
Dim n As TimeSpan
i = Now
o = Now
n = i.Subtract(o)
Console.WriteLine(n.Days)
End Sub
End Module

Also this, you suck ass OP.

I don't understand how it could take an hour. What kind of mom's spaghetti code flinging poo in loo are you?

Of course the bird is black.

public static System.out.println dayCalc(){
int answer =Timestamp - Timestamp2;
return system.out.println("answer");

}

public static System.out.println dayCalc(){
int answer =Timestamp - Timestamp2;
return system.out.println("answer");

}

ftfy

Sorry, I tried.

You inspired me to write a version with no loops. The idea is to index everything from March 1, making leap year calculation trivial. Then adjusting for the actual month with a fixed array, followed by a final adjustment if it's Jan or Feb of a leap year.

#include
#include

typedef struct {
int year; /* 0 to 9999 */
int month; /* 1 to 12 */
int day; /* 1 to 31 */
} date;

/* Determines leapdays between March 1 of end and start years*/
int leapdays(int year1, int year2)
{
int leaps = year2/4 - year1/4;
leaps -= year2/100 - year1/100;
leaps += year2/400 - year1/400;
return leaps;
}

/*Returns 1 if month is Jan or Feb and it is a leap year*/
int leapyear(date* d)
{
int year = d->year;
return d->month < 3 && !(year % 4) && ((year % 100) || !(year % 400));
}

// days between first of month and March 1, Jan = 0
int daystomarch[12] = {-59,-28,0,31,61,92,122,153,184,214,245,275};

int timespan(date* from, date* to) {
int days = 365*(to->year - from->year) + to->day - from->day + daystomarch[to->month - 1] - daystomarch[from->month - 1];
days += leapdays(from->year, to->year);
days += leapyear(from) - leapyear(to);
return days;
}

int main(void) {
date a = {1970, 1, 1};
date b = {2016, 7, 13};
int days = timespan(&a, &b);
printf("%d\n", days);
return EXIT_SUCCESS;
}

var a = new Date(1468473133)
var b = new Date(1467331200)
var diffDays = Math.round(Math.abs(a.getTime()-b.getTime())/(60*60*24))
console.log(diffDays)

var moment = require('moment');

var daysDifference = moment(date1).difference(date2).days();

well nevermind that javascript uses milliseconds, forgot a 1000 here and there, whatever

:^)

Ruby one-liner (as expected):

require 'date'

diff = Date.parse("25/03/2016") - Date.parse("30/05/2015")
p diff.to_i

And before somebody nags:

If OP can include two libraries, I can include one.

Check out reddit.com/r/dailyprogrammer. I like it for similar reasons. They have 3 challenges a week: easy on Monday, intermediate on Wednesday, and hard-on Friday.
>inb4 get out leddit

Same goes for me.

I really to annoy C fags (like OP) with my beautifull Ruby statements.. :)

But I also enjoy looking at the solutions in Python, Perl and Haskell, because it's often astonishing smart.

Even though it's only micro tasks (unless you bother with defining structs and reinventing the wheel, like the C fags) it's still amusing.

dif.days

I'd put the leapyear function like this:

int leapyear(int year) {
if (year % 400 == 0) return 1;
if (year % 100 == 0) return 0;
if (year % 4 == 0) return 1;
return 0;
}

Forgot to say it's in R

What's the difference?

Using the time crate, in Rust:

extern crate time;

use time::{strptime, Tm};

fn days_between(first: &Tm, second: &Tm) -> i64 {
(*second - *first).num_days()
}

fn main() {
let format = "%d.%m.%Y";

let n = strptime("1.5.2015", format).unwrap();
let s = strptime("1.11.2016", format).unwrap();

println!("{}", days_between(&n, &s));
}

Not using the curly braces, therefore more readable.

You mean this?

With ThisWorkbook.Sheets(1)
MsgBox DateDiff("d", CDate(.Range("A1")), CDate(.Range("A2")))
End With

Scratch that, the chrono crate is much better for that:

extern crate chrono;

use chrono::{UTC, TimeZone};

fn main() {
let n = UTC.ymd(2015, 7, 11);
let s = UTC::today();
println!("{}", (s - n).num_days());
}

>Rust

Easier in R
day.dif

Module Module1
Sub Main()
Dim date1 = #5/1/2015#
Dim date2 = #7/1/2015#
Console.WriteLine(DateDiff(DateInterval.Day, date1, date2))
End Sub
End Module


Sorry plebs but Visual Basic is just too superior.

Monday

Tuesday

>Not using the curly braces, therefore more readable.

You don't read curly braces.

int you()
{
are()
{
// ..a bracket fag
}
}

#blackcrowsmatter

>that fucking leapyear function

What's the point? You either just do % 4 or if you really wanna be smartass you can check last two bits

lambda a,b: a-b/86400

var moment = require('moment')

var OPISAFAG = function(startDate, endDate) {
return endDate.diff(startDate, 'days')
}

var startDate = moment('2013-5-11 8:73:18', 'YYYY-M-DD HH:mm:ss')
var endDate = moment('2013-5-11 10:73:18', 'YYYY-M-DD HH:mm:ss')

console.log(OPISAFAG(startDate, endDate));