I want to get year from date in mongodb.
This is my simple script :
DateTime sDate = DateTime.SpecifyKind(new DateTime(2013, 1, 15, 00, 00, 00), DateTimeKind.Utc);
DateTime eDate = DateTime.SpecifyKind(new DateTime(2014, 12, 31, 23, 59, 59), DateTimeKind.Utc);
string map = # "function(){
emit (_id.OwnerId, _id.Date {
Date: Value.Date.getYear(),
PurchaseAmount: parseFloat(Value.PurchaseAmount),
PurchaseReturnAmount: parseFloat(Value.PurchaseReturnAmount),
TotalAmount: parseFloat(Value.TotalAmount)
});
}";
string reduce = # "function (key, values) {
var outValue = {purchsaeAmount:0 , purchaseReturnAmount:0, totalAmount:0 }
values.forEach(function (value) {
outValue.PurchaseAmount +=parseFloat(value.PurchaseAmount);
outValue.PurchaseReturnAmount +=parseFloat(value.PurchaseReturnAmount);
outValue.TotalAmount +=parseFloat(value.TotalAmount);
});
return outValue;
}";
In map I want to get only year, how can I get start year(2013) and end year(2014)?
var d = new Date();
var n = d.getFullYear();
var next_year = d.getFullYear()+1
out:
2013 2014
Related
I writted code for automatical changer of shifts. Night shift, morning shift, etc. Everything works fine. Current problem with this is when is new month. 5 Month has 31 days. This month have 30 days.
My current code is this:
DateTime lastTime = _lastDateOfLoading;
DateTime tempTime = Convert.ToDateTime(hourOfLoading);
DateTime targetTime = new DateTime(DateOfLoading.Year, DateOfLoading.Month, DateOfLoading.Day, tempTime.Hour, tempTime.Minute, tempTime.Second);
DateTime currentTime = DateTime.Now;
bool tt1 = false;
bool tt2 = false;
bool lt1 = false;
bool lt2 = false;
DateTime lt1Min = new DateTime(currentTime.Year, currentTime.Month, lastTime.Day, 22, 00, 00);
DateTime lt1Max = new DateTime(currentTime.Year, currentTime.Month, lastTime.Day, 23, 59, 59, 999);
DateTime lt2Min = new DateTime(currentTime.Year, currentTime.Month, lastTime.Day, 00, 00, 00);
DateTime lt2Max = new DateTime(currentTime.Year, currentTime.Month, lastTime.Day, 06, 00, 00);
DateTime tt1Min = new DateTime(currentTime.Year, currentTime.Month, targetTime.Day, 22, 00, 00);
DateTime tt1Max = new DateTime(currentTime.Year, currentTime.Month, targetTime.Day, 23, 59, 59, 999);
DateTime tt2Min = new DateTime(currentTime.Year, currentTime.Month, targetTime.Day, 00, 00, 00);
DateTime tt2Max = new DateTime(currentTime.Year, currentTime.Month, targetTime.Day, 06, 00, 00);
if ((lastTime >= lt1Min
&& (lastTime < lt1Max)))
{
lt1 = true;
}
etc ..
It end on lt1Min = new DateTime ...
I think it happen because is june. And lastTime is 22.5.
Any advice how to solve this thing? Nothing helped on current threads here on stackoverlow.
Thank you.
I’m looking for a way to find holes in a schedule, times that does not have a booking.
I have a simple class in C# that looks like:
DateTime StartTime { get; set; }
Datetime EndTime { get; set; }
public int User_ID { get; set; }
The same class is used for the bookings aswell.
Let’s assume I have these objects:
Schedule: StartTime "2017-03-14 08:00" - EndTime "2017-03-14 16:00" (8 hours)
Booking: StartTime "2017-03-14 09:00" - Endtime "2017-03-14 10:00" (1 hour)
My final result from this would be 2 objects that represents the “free time”:
Free: StartTime "2017-03-14 08:00" EndTime: "2017-03-14 09:00" (1 hour)
Free: StartTime "2017-03-14 10:00" EndTime: "2017-03-14 16:00"(6 hour)
How would I check this in C#?
I'm thinking about looping the Schedule and split them on start/end of each booking, but I'm not sure how to do it.
It is easier than I thought... Note that this code isn't optimized, and this algorithm isn't probably very optimizable:
public class TimeSegment
{
public readonly DateTime StartTime;
public readonly DateTime EndTime;
public TimeSegment(DateTime startTime, DateTime endTime)
{
StartTime = startTime;
EndTime = endTime;
}
public TimeSegment[] Subtract(TimeSegment other)
{
// 8-10 Subtract 10-11 = 8-10
if (StartTime > other.EndTime || other.StartTime > EndTime)
{
// If there is no intersection, we return { this }
// (no subtraction)
return new[] { this };
}
if (StartTime >= other.StartTime)
{
// 8-10 Subtract 8-10 = (nothing)
// 8-10 Subtract 7-11 = (nothing)
if (EndTime <= other.EndTime)
{
// Total subtraction, nothing remains!
return new TimeSegment[0];
}
else
{
// 8-10 Subtract 7-9 = 9-10
return new[] { new TimeSegment(other.EndTime, EndTime) };
}
}
// 8-12 Subtract 9-13 = 8-9
if (EndTime <= other.EndTime)
{
return new[] { new TimeSegment(StartTime, other.EndTime) };
}
// 8-12 Subtract 9-11 = 8-9, 11-12
// Complete case: two TimeSegments returned
return new[] { new TimeSegment(StartTime, other.StartTime), new TimeSegment(other.EndTime, EndTime) };
}
public override string ToString()
{
return string.Format("{0}-{1}", StartTime, EndTime);
}
}
And then:
var schedules = new List<TimeSegment> { new TimeSegment(new DateTime(2017, 03, 14, 08, 00, 00), new DateTime(2017, 03, 14, 16, 00, 00)) };
var bookings = new List<TimeSegment>
{
new TimeSegment(new DateTime(2017, 03, 14, 09, 00, 00), new DateTime(2017, 03, 14, 10, 00, 00)),
new TimeSegment(new DateTime(2017, 03, 14, 12, 00, 00), new DateTime(2017, 03, 14, 14, 00, 00)),
new TimeSegment(new DateTime(2017, 03, 14, 13, 00, 00), new DateTime(2017, 03, 14, 15, 00, 00)),
};
foreach (TimeSegment booking in bookings)
{
var schedulesNew = new List<TimeSegment>();
foreach (TimeSegment schedule in schedules)
{
var diff = schedule.Subtract(booking);
schedulesNew.AddRange(diff);
}
schedules = schedulesNew;
}
There "core" of this is a Subtract function that given a TimeSegment subtracts from this another TimeSegment, returning 0, 1 or 2 TimeSegments... Then iteratively we subtract all the bookings from the TimeSegments that we produced from the previous booking.
Getting InvalidOperationException or sometimes TargetInvocationException, When Control reaches at this function the Exception thrown.
private void setreminders()
{
DateTime Date1 = new DateTime(2014, 02, 10, 06, 00, 00);
DateTime Date2 = new DateTime(2014, 02, 10, 07, 30, 00);
Reminder _Reminder1 = new Reminder("TodoReminder")
{
BeginTime = Date1,
Title = "Diet Reminder",
Content = "Its time to wake up, Have a Glass of Water and Start Excercising like YOGA & PRANAYAMA",
};
_Reminder1.RecurrenceType = RecurrenceInterval.Daily;
ScheduledActionService.Add(_Reminder1);
Reminder _Reminder2 = new Reminder("TodoReminder")
{
BeginTime = Date2,
Title = "Diet Reminder",
Content = "Drink a Glass of Water",
};
_Reminder2.RecurrenceType = RecurrenceInterval.Daily;
ScheduledActionService.Add(_Reminder2);
}
I have already asked a different question regarding Sorting Date Time and got help from another user to pass my values. I am using a for loop like below, but definitely am wrong here because the code brings the value one by one rather than sorting.
public class Break
{
public DateTime MealStart { get; set; }
public DateTime MealEnd { get; set; }
}
my main class
IList<DateTime> starts = new List<DateTime>();
IList<DateTime> ends = new List<DateTime>();
DateTime breakStart1 = new DateTime(2012, 02, 15, 12, 30, 00); // 15/02/12 12.30PM
DateTime breakEnd1 = new DateTime(2012, 02, 15, 13, 30, 00); // 15/02/12 01.30PM
DateTime breakStart2 = new DateTime(2012, 02, 15, 11, 00, 00); // 15/02/12 11.00AM
DateTime breakEnd2 = new DateTime(2012, 02, 15, 12, 00, 00); // 15/02/12 12.00PM
DateTime breakStart3 = new DateTime(2012, 02, 15, 12, 00, 00); // 15/02/12 12.00PM
DateTime breakEnd3 = new DateTime(2012, 02, 15, 01, 00, 00); // 15/02/12 01.00PM
starts.Add(breakStart1);
starts.Add(breakStart2);
starts.Add(breakStart3);
ends.Add(breakEnd1);
ends.Add(breakEnd2);
ends.Add(breakEnd3);
for (int i = 0; i < starts.Count; i++)
{
var breaks = new List<Break>()
{
//for (int j= 0; j<starts.Count; j++)
//{
new Break()
{
MealStart = starts[i],
MealEnd = ends[i]
}
// }
};
var ordered = breaks.OrderBy(s => s.MealStart);
foreach (var ord in ordered)
{
System.Console.WriteLine(ord.MealStart);
System.Console.WriteLine(ord.MealEnd);
}
}
I am expecting a result like below
breakStart1 = 15/02/12 11.00AM
breakEnd1= 15/02/12 12.00PM
breakStart2 = 15/02/12 12.00PM
breakEnd2= 15/02/12 01.00PM
breakStart3 = 15/02/12 12.30PM
breakEnd3= 15/02/12 01.30PM
but it's not because of the for loop.
You are creating breaks after ever loop, you need to do this outside of the loop like this:
IList<DateTime> starts = new List<DateTime>();
IList<DateTime> ends = new List<DateTime>();
DateTime breakStart1 = new DateTime(2012, 02, 15, 12, 30, 00); // 15/02/12 12.30PM
DateTime breakEnd1 = new DateTime(2012, 02, 15, 13, 30, 00); // 15/02/12 01.30PM
DateTime breakStart2 = new DateTime(2012, 02, 15, 11, 00, 00); // 15/02/12 11.00AM
DateTime breakEnd2 = new DateTime(2012, 02, 15, 12, 00, 00); // 15/02/12 12.00PM
DateTime breakStart3 = new DateTime(2012, 02, 15, 12, 00, 00); // 15/02/12 12.00PM
DateTime breakEnd3 = new DateTime(2012, 02, 15, 01, 00, 00); // 15/02/12 01.00PM
starts.Add(breakStart1);
starts.Add(breakStart2);
starts.Add(breakStart3);
ends.Add(breakEnd1);
ends.Add(breakEnd2);
ends.Add(breakEnd3);
List<Break> breaks = new List<Break>();
for (int i = 0; i < starts.Count; i++)
{
breaks.Add(new Break()
{
MealStart = starts[i],
MealEnd = ends[i]
});
}
var ordered = breaks.OrderBy(s => s.MealStart);
foreach (var ord in ordered)
{
System.Console.WriteLine(ord.MealStart);
System.Console.WriteLine(ord.MealEnd);
}
Since #Corylulu beat me to the punch on the basic issue, here's a different method that is slightly shorter:
IEnumerable<Break> breaks =
starts.Zip(ends, (s, e) => new Break { MealStart = s, MealEnd = e })
.OrderBy(b => b.MealStart);
foreach (Break brk in breaks)
Console.WriteLine("Start: {0}\tEnd: {1}", brk.BreakStart, brk.BreakEnd);
The IEnumerable.Zip method takes a pair of IEnumerables and a transform function and produces an output IEnumerable containing the results of calling the transform function with members of each input IEnumerable. You could convert it to a List<Break> with a ToList() at the end of course.
I'm trying to find missing dates between two DateTime variables for a collection of DateTimes.
For example.
Collection
2010-01-01
2010-01-02
2010-01-03
2010-01-05
DateRange
2010-01-01 -> 2010-01-06
would give me a List<DateTime> of
2010-01-04
2010-01-06
I can think of a few was of implementing this but nothing clean and simple
Any ideas?
I can think of a lot of ways of implementing this, e.g.:
DateTime[] col = { new DateTime(2010, 1, 1),
new DateTime(2010, 1, 2),
new DateTime(2010, 1, 3),
new DateTime(2010, 1, 5)};
var start = new DateTime(2010, 1, 1);
var end = new DateTime(2010, 1, 6);
var range = Enumerable.Range(0, (int)(end - start).TotalDays + 1)
.Select(i => start.AddDays(i));
var missing = range.Except(col);
And you could put the range-stuff into an Extension-Method
public static class extensions
{
public static IEnumerable<DateTime> Range(this DateTime startDate, DateTime endDate)
{
return Enumerable.Range(0, (int)(endDate - startDate).TotalDays + 1)
.Select(i => startDate.AddDays(i));
}
}
Then it would be simply
DateTime[] col = { new DateTime(2010, 1, 1),
new DateTime(2010, 1, 2),
new DateTime(2010, 1, 3),
new DateTime(2010, 1, 5)};
var start = new DateTime(2010, 1, 1);
var end = new DateTime(2010, 1, 6);
var missing = start.Range(end).Except(col);
But maybe this is not a high-performance-solution :-)
Depending on exactly what you are looking for and the sizes of the sets of data. A simple way would be to load the dates into a collection, then use a simple loop. I'll add a code sample here in a second.
DateTime currentDate = new DateTime(2010, 1, 1);
DateTime endDate = new DateTime(2010, 1, 6);
List<DateTime> existingDates = new List<DateTime>; //You fill with values
List<DateTime> missingDates = new List<DateTime>;
while(currentDate <= endDate)
{
if(existingDates.contains(currentDate))
missingDates.Add(currentDate);
//Increment date
currentDate = currentDate.AddDays(1);
}
Using this example you just need to load "existingDates" with the proper values, then the "missingDates" list will have your results
In .NET 2.0 :)
static void Main(string[] args)
{
List<DateTime> dates = new List<DateTime>();
dates.Add(new DateTime(2010, 01, 27));
dates.Add(new DateTime(2010, 01, 30));
dates.Add(new DateTime(2010, 01, 31));
dates.Add(new DateTime(2010, 02, 01));
DateTime startDate = new DateTime(2010, 01, 25);
DateTime endDate = new DateTime(2010, 02, 02);
List<DateTime> missingDates = new List<DateTime>(GetMissingDates(dates, startDate, endDate));
}
private static IEnumerable<DateTime> GetMissingDates(IList<DateTime> dates, DateTime startDate, DateTime endDate)
{
TimeSpan _timeStamp = endDate - startDate;
DateTime _tempDateTime = startDate;
IList<DateTime> _dateTimeRange = new List<DateTime>();
IList<DateTime> _missingDates = new List<DateTime>();
for (int i = 0; i <= _timeStamp.Days; i++)
{
_dateTimeRange.Add(_tempDateTime);
_tempDateTime = _tempDateTime.AddDays(1);
}
foreach (DateTime dt in _dateTimeRange)
{
if (!dates.Contains(dt))
yield return dt;
}
}
Lazy evaluated helper method aids in generating the list of dates to compare with. Might want to performance profile this method for large collections.
void Main()
{
var dates = new[] {new DateTime(2000,1,1), new DateTime(2000,1,5)};
DateHelper.Range(new DateTime(2000,1,1), new DateTime(2000,1,5)).Except(dates).Dump();
}
// Define other methods and classes here
public static class DateHelper {
public static IEnumerable<DateTime> Range(DateTime start, DateTime end) {
var days = end.Subtract(start).Days;
var next = start;
for(var i = 0; i<days; i++) {
next = next.AddDays(1);
yield return next;
}
}
}
var dates = new List<DateTime>
{
new DateTime( 2010, 01, 01 ),
new DateTime( 2010, 01, 02 ),
new DateTime( 2010, 01, 03 ),
new DateTime( 2010, 01, 05 )
};
var targetDate = new DateTime( 2010, 01, 01 );
var missingDates = new List<DateTime>();
while ( targetDate <= new DateTime( 2010, 01, 06 ) )
{
if ( !dates.Contains( targetDate ) )
missingDates.Add( targetDate );
targetDate = targetDate.AddDays( 1 );
}
foreach ( var date in missingDates )
Debug.WriteLine( date.ToString() );
If you were thinking of solving this is LINQ, I do not believe it is possible unless you also had a list of all dates between the min and max date. In SQL, this amounts to a calendar table that contains all dates across a given time period.
Here is a LINQ solution where I create the Calendar list I mentioned above and then query for missing dates:
var dates = new List<DateTime>
{
new DateTime( 2010, 01, 01 ),
new DateTime( 2010, 01, 02 ),
new DateTime( 2010, 01, 03 ),
new DateTime( 2010, 01, 05 )
};
var calendar = new List<DateTime>();
var targetDate = new DateTime( 2010, 01, 01 );
while ( targetDate <= new DateTime( 2010, 01, 06 ) )
{
calendar.Add( targetDate );
targetDate = targetDate.AddDays( 1 );
}
var missingDates = ( from date in calendar
where !dates.Contains( date )
select date ).ToList();
foreach ( var date in missingDates )
Debug.WriteLine( date.ToString() );