Calculate the number of weekdays between 2 dates in R

Date1 <- as.Date(“2011-01-30”) Date2 <- as.Date(“2011-02-04”) sum(!weekdays(seq(Date1, Date2, “days”)) %in% c(“Saturday”, “Sunday”)) EDIT: And Zach said, let there be Vectorize 🙂 Dates1 <- as.Date(“2011-01-30”) + rep(0, 10) Dates2 <- as.Date(“2011-02-04”) + seq(0, 9) Nweekdays <- Vectorize(function(a, b) sum(!weekdays(seq(a, b, “days”)) %in% c(“Saturday”, “Sunday”))) Nweekdays(Dates1, Dates2)

Aggregate Daily Data to Month/Year intervals

I’d do it with lubridate and plyr, rounding dates down to the nearest month to make them easier to plot: library(lubridate) df <- data.frame( date = today() + days(1:300), x = runif(300) ) df$my <- floor_date(df$date, “month”) library(plyr) ddply(df, “my”, summarise, x = mean(x))

Parsing of Ordered Timestamps in Local Time (to UTC) While Observing Daylight Saving Time

In C#: // Define the input values. string[] input = { “2013-11-03 00:45:00”, “2013-11-03 01:00:00”, “2013-11-03 01:15:00”, “2013-11-03 01:30:00”, “2013-11-03 01:45:00”, “2013-11-03 01:00:00”, “2013-11-03 01:15:00”, “2013-11-03 01:30:00”, “2013-11-03 01:45:00”, “2013-11-03 02:00:00”, }; // Get the time zone the input is meant to be interpreted in. TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById(“Eastern Standard Time”); // Create an array … Read more

Why are date/time values interpreted incorrectly when patching/saving?

Date/time values are being casted/parsed in a locale aware fashion Update: this is the default behavior with the CakePHP application template versions prior to 3.2.5. As of 3.2.5 locale parsing is not enabled by default anymore, which will make the date/time marshalling logic expect a default format of Y-m-d H:i:s instead. In the marshalling process, … Read more

Convert 12-hour date/time to 24-hour date/time

Using Perl and hand-crafted regexes instead of facilities like strptime: #!/bin/perl -w while (<>) { # for date times that don’t use leading zeroes, use this regex instead: # (?:\d{1,2}/\d{1,2}/\d{4} )(\d{1,2})(?::\d\d:\d\d) (AM|PM) while (m%(?:\d\d/\d\d/\d{4} )(\d\d)(?::\d\d:\d\d) (AM|PM)%) { my $hh = $1; $hh -= 12 if ($2 eq ‘AM’ && $hh == 12); $hh += 12 … Read more

How do you specify the date format used when JAXB marshals xsd:dateTime?

You can use an XmlAdapter to customize how a date type is written to XML. package com.example; import java.text.SimpleDateFormat; import java.util.Date; import javax.xml.bind.annotation.adapters.XmlAdapter; public class DateAdapter extends XmlAdapter<String, Date> { private final SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”); @Override public String marshal(Date v) throws Exception { synchronized (dateFormat) { return dateFormat.format(v); } } @Override public … Read more