DirtyBit
DirtyBit

Reputation: 16772

Extracting DateTime from Date to get DateTime

What I have is;

  1. A list of weekendDates in a format of 11/4/2017 12:00:00 AM
  2. A list of late comings lateDateTimeList in a format of 11/4/2017 02:40:03 PM

and I am trying to exclude all the weekends from the late comings, So I have a list that has all the date and time both (without weekendDates) since I need the time later.

for weekendDates:

// Build list of weekdates and weekend dates
            List<DateTime> weekDates = new List<DateTime>();
            List<DateTime> weekendDates = new List<DateTime>();
            int days = (EndDate - StartDate).Days;
            for (int i = 0; i < days; i++)
            {
                DateTime date = StartDate.AddDays(i);
                if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
                {
                    weekendDates.Add(date);
                    continue;
                }
                weekDates.Add(date);
            }

late coming list:

var fetchAllrec = (from m in db.D2L
                            where m.RId == ENum && m.Date >= StartDate && m.Date <= EndDate
                            select m).Distinct();

            var onerecord = fetchAllrec
                    .GroupBy(dt => dt.Date)
                    .Select(z => z.OrderBy(y => y.Date).FirstOrDefault())
                    .ToList();

//All the dates he was late compared to his AT
            var late = (from d in onerecord
                        where d.DateTime.Value.Hour >= ArT
                        select d).ToList();

WithoutWeekends:

//All the late dates without weekends
            IEnumerable<DateTime> lateDateTimeList = late.Select(m => m.DateTime.Value).ToList(); 
            //^^^Change this to m.Date to get the 'latDateTimeList' dates without weekends but then it wont calculate
            //the minutes since it will have all the Dates in the 'lateDateTimeList' ONLY!
            IEnumerable<DateTime> lateDateTimeWithoutWeekendsList =  lateDateTimeList.Except(weekendDates).ToList();


            //get latecoming DateTimes list here
            var lateComings = new List<DateTime>();


            foreach (var g in lateDateTimeWithoutWeekendsList)      
            {
                lateComings.Add(g);    // IT MUST HAVE DATE TIME HERE NOT JUST DATE
            }


            countLates = lateComings.Count();

           //list of time differences 
            var timeDiff = new List<TimeSpan>();

            foreach (DateTime ts in lateComings)
            {

                if (ts.TimeOfDay > TimeSpan.FromHours(AT))
                {
                    timeDiff.Add(ts.TimeOfDay - TimeSpan.FromHours(AT));
                }
            }
 //fetching the minutes from the above list


            var minutesList = timeDiff.Select(t => Convert.ToInt32(t.TotalMinutes)).ToList();

What the above code does is that it does not exclude the weekendDates, i.e. this part gives complete list including weekendDates i.e. the list that was in lateDateTimeList

IEnumerable<DateTime> lateDateTimeWithoutWeekendsList = lateDateTimeList.Except(weekendDates).ToList();

OUTPUT:

11/2/2017 10:53:00 AM
11/3/2017 11:10:03 AM
11/4/2017 01:12:13 PM
11/5/2017 12:11:10 PM

but if I change this;

IEnumerable<DateTime> lateDateTimeList = late.Select(m => m.DateTime.Value).ToList(); 

to

IEnumerable<DateTime> lateDateTimeList = late.Select(m => m.Date.Value).ToList(); 

it gives me all the dates WITHOUT time.

OUTPUT:

11/2/2017 12:00:00 AM
11/3/2017 12:00:00 AM

Upvotes: 0

Views: 156

Answers (2)

Felix D.
Felix D.

Reputation: 5093

How about that:

static void Main(string[] args)
{

    Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

    List<DateTime> dates = new List<DateTime>();

    for(int i = 1; i <= 50; i++)
    {
        DateTime date = DateTime.Now.AddDays(i);
        dates.Add(date);
        Trace.WriteLine(date.ToString("dd.MM.yyyy HH:mm:ss dddd"));
    }

    List<DateTime> filteredDates = dates.Where(date => 
                                               !(date.DayOfWeek == DayOfWeek.Saturday ||
                                                 date.DayOfWeek == DayOfWeek.Sunday))
                                        .ToList();

    Trace.WriteLine("------NO-WEEKENDS----------");

    filteredDates.ForEach(date => Trace.WriteLine(date.ToString("dd.MM.yyyy HH:mm:ss dddd")));

    Console.ReadKey();
}

Using Linq Enumerable.Where

Output:

07.12.2017 08:40:32 Thursday
08.12.2017 08:40:32 Friday
09.12.2017 08:40:32 Saturday
10.12.2017 08:40:32 Sunday
11.12.2017 08:40:32 Monday
12.12.2017 08:40:32 Tuesday
13.12.2017 08:40:32 Wednesday
14.12.2017 08:40:32 Thursday
15.12.2017 08:40:32 Friday
16.12.2017 08:40:32 Saturday
17.12.2017 08:40:32 Sunday
18.12.2017 08:40:32 Monday
19.12.2017 08:40:32 Tuesday
20.12.2017 08:40:32 Wednesday
21.12.2017 08:40:32 Thursday
22.12.2017 08:40:32 Friday
23.12.2017 08:40:32 Saturday
24.12.2017 08:40:32 Sunday
25.12.2017 08:40:32 Monday
26.12.2017 08:40:32 Tuesday
27.12.2017 08:40:32 Wednesday
28.12.2017 08:40:32 Thursday
29.12.2017 08:40:32 Friday
30.12.2017 08:40:32 Saturday
31.12.2017 08:40:32 Sunday
01.01.2018 08:40:32 Monday
02.01.2018 08:40:32 Tuesday
03.01.2018 08:40:32 Wednesday
04.01.2018 08:40:32 Thursday
05.01.2018 08:40:32 Friday
06.01.2018 08:40:32 Saturday
07.01.2018 08:40:32 Sunday
08.01.2018 08:40:32 Monday
09.01.2018 08:40:32 Tuesday
10.01.2018 08:40:32 Wednesday
11.01.2018 08:40:32 Thursday
12.01.2018 08:40:32 Friday
13.01.2018 08:40:32 Saturday
14.01.2018 08:40:32 Sunday
15.01.2018 08:40:32 Monday
16.01.2018 08:40:32 Tuesday
17.01.2018 08:40:32 Wednesday
18.01.2018 08:40:32 Thursday
19.01.2018 08:40:32 Friday
20.01.2018 08:40:32 Saturday
21.01.2018 08:40:32 Sunday
22.01.2018 08:40:32 Monday
23.01.2018 08:40:32 Tuesday
24.01.2018 08:40:32 Wednesday
25.01.2018 08:40:32 Thursday

------NO-WEEKENDS----------

07.12.2017 08:40:32 Thursday
08.12.2017 08:40:32 Friday
11.12.2017 08:40:32 Monday
12.12.2017 08:40:32 Tuesday
13.12.2017 08:40:32 Wednesday
14.12.2017 08:40:32 Thursday
15.12.2017 08:40:32 Friday
18.12.2017 08:40:32 Monday
19.12.2017 08:40:32 Tuesday
20.12.2017 08:40:32 Wednesday
21.12.2017 08:40:32 Thursday
22.12.2017 08:40:32 Friday
25.12.2017 08:40:32 Monday
26.12.2017 08:40:32 Tuesday
27.12.2017 08:40:32 Wednesday
28.12.2017 08:40:32 Thursday
29.12.2017 08:40:32 Friday
01.01.2018 08:40:32 Monday
02.01.2018 08:40:32 Tuesday
03.01.2018 08:40:32 Wednesday
04.01.2018 08:40:32 Thursday
05.01.2018 08:40:32 Friday
08.01.2018 08:40:32 Monday
09.01.2018 08:40:32 Tuesday
10.01.2018 08:40:32 Wednesday
11.01.2018 08:40:32 Thursday
12.01.2018 08:40:32 Friday
15.01.2018 08:40:32 Monday
16.01.2018 08:40:32 Tuesday
17.01.2018 08:40:32 Wednesday
18.01.2018 08:40:32 Thursday
19.01.2018 08:40:32 Friday
22.01.2018 08:40:32 Monday
23.01.2018 08:40:32 Tuesday
24.01.2018 08:40:32 Wednesday
25.01.2018 08:40:32 Thursday

Upvotes: 1

Patrick Artner
Patrick Artner

Reputation: 51643

  using System;
  using System.Collections.Generic;
  using System.Linq;

  static class DateTimeExtension
  {
    public static bool IsWeekend (this DateTime dt) 
        => dt.DayOfWeek == DayOfWeek.Saturday || dt.DayOfWeek == DayOfWeek.Sunday;

    public static Func<DateTime, bool> IsWeekend_2 = 
        dt => dt.DayOfWeek == DayOfWeek.Saturday || dt.DayOfWeek == DayOfWeek.Sunday;    
  }

  class Program
  {
    Func<DateTime, bool> IsWeekend = dt => dt.DayOfWeek == DayOfWeek.Saturday || dt.DayOfWeek == DayOfWeek.Sunday;

    static void PrintDates (IList<DateTime> dts) => Console.WriteLine (string.Join ("", dts.Select (item => item.ToString () + "      Weekend: " + (item.IsWeekend () ? "Yes" : "No") + "\n"))) ;
    static Random r = new Random ();
    static void Main (string[] args)
    {
      // Data
      var dates = new List<DateTime> ();
      Enumerable.Range (0, 50)
          .ToList ()
          .ForEach (n => dates.Add (DateTime.Now.AddDays (n).AddHours(r.NextDouble() * 12)));

      // no weekends
      var datesWithoutWeekends = dates.Where (myDate => !myDate.IsWeekend ()).ToList ();

      // also no weekends
      var datesWithoutWeekends_2 = dates.Where (myDate => !DateTimeExtension.IsWeekend_2 (myDate)).ToList ();

      // still no weekends
      var datesWithoutWeekends_3 = dates.Where (myDate =>myDate.DayOfWeek != DayOfWeek.Sunday && myDate.DayOfWeek == DayOfWeek.Saturday).ToList ();


      PrintDates (dates);
      Console.WriteLine ();
      PrintDates (datesWithoutWeekends);
      Console.ReadLine ();
    }
  }

Just filter the weekends on the fly - f.e. with an Extension - or a Predicate (do not use Predicase, use Func - they are the same but Func is func'ier) or do it inline.

Format the output however you want using DateTime Customn Format

Output (German default locale dd.mm.YYYY):

06.12.2017 16:40:13      Weekend: No
07.12.2017 15:06:42      Weekend: No
08.12.2017 12:09:55      Weekend: No
09.12.2017 10:19:20      Weekend: Yes
10.12.2017 19:51:28      Weekend: Yes
11.12.2017 12:18:26      Weekend: No
15.12.2017 17:11:18      Weekend: No
16.12.2017 15:45:32      Weekend: Yes
17.12.2017 11:39:25      Weekend: Yes
18.12.2017 17:34:05      Weekend: No
19.12.2017 16:25:51      Weekend: No
20.12.2017 12:11:49      Weekend: No
21.12.2017 16:37:07      Weekend: No
22.12.2017 17:56:36      Weekend: No
23.12.2017 08:37:13      Weekend: Yes
24.12.2017 09:22:52      Weekend: Yes
25.12.2017 20:01:19      Weekend: No
26.12.2017 13:24:30      Weekend: No


06.12.2017 16:40:13      Weekend: No
07.12.2017 15:06:42      Weekend: No
08.12.2017 12:09:55      Weekend: No
11.12.2017 12:18:26      Weekend: No
12.12.2017 20:00:09      Weekend: No
13.12.2017 20:12:48      Weekend: No
14.12.2017 17:03:31      Weekend: No
15.12.2017 17:11:18      Weekend: No
18.12.2017 17:34:05      Weekend: No
19.12.2017 16:25:51      Weekend: No
20.12.2017 12:11:49      Weekend: No
21.12.2017 16:37:07      Weekend: No
22.12.2017 17:56:36      Weekend: No
25.12.2017 20:01:19      Weekend: No
26.12.2017 13:24:30      Weekend: No

Upvotes: 0

Related Questions