Wednesday, April 24, 2019

Function to calculate the difference between two dates using C#



In this article, we are going to look at how to calculate the date difference between two dates in C#. Like in other OOP languages, C# has two data types to store the results. 
  • DateTime Structure
  • TimeSpan Structure


DateTime Struct

DateTime Structure is a child of System namespace and no any other external libraries needed for this. Normally DateTime constructor takes minimum 3 parameters; year, month and date. Further, the constructor can be overloaded with multiple parameters to get the time range. To take the date and time ranges, we can use Calendar data type as well. But it is recommended to use DateTime struct, as it is the most efficient and accurate way.
            /* Year=2019 Month=03 Date=01*/
            DateTime datetime = new DateTime(2019, 03, 01);

            /* Year=2019 Month=03 Date=01 Hour=00 Minute=00 Second=00*/
            DateTime datetime = new DateTime(2019, 03, 01, 00, 00, 00);

TimeSpan Struct
TimeSpan is considered as the data type which is used to store the length of time. Just like the DateTime struct, it is also implemented as a struct and has several overloaded constructors. TimeSpan Structure too is a child of System namespace and in our example, we are going to use it to store the difference between the dates. Hence, TimeSpan can be used for many date manipulations as shown below.
           TimeSpan days = TimeSpan.FromDays(1);               
            TimeSpan hours = TimeSpan.FromHours(1);
            TimeSpan mins = TimeSpan.FromMinutes(1);

            Console.WriteLine(days); 
            Console.WriteLine(hours);
            Console.WriteLine(mins);

What is a Struct?

Unlike classes, structs can be used without instantiating with a new keyword. Structs provide better performance when the value-type collection to be grouped is small. Structs cannot inherit either from the same struct type or even a different class. To define a struct, we need to use the word struct before the name of the class.
          public struct Employee {
            public int age;

            public setAge(int newAge)
            {
                age = newAge;
            }
        }
In this example, we are calculating the difference between the dates using 2 methods.
·         Date Difference by Subtract() Method
·         Date Difference by Subtraction(-) Operator

Date Difference by Subtract() Method
DateTime structure has its own predefined method Subtract() which takes a date as the argument and returns the difference between the specified dates. It results in TimeSpan value and we can use different properties defined by the TimeSpan to display the output as per our requirement.
class DateDifference
{
  public void getDateDiff(DateTime date1, DateTime date2) {
     DateTime oldDate = new System.DateTime(date1.Year, date1.Month,
                          date1.Day, date1.Hour, date1.Minute, date1.Second);
     DateTime newDate = new System.DateTime(date2.Year, date2.Month,
                          date2.Day, date2.Hour, date2.Minute, date2.Second);
     TimeSpan dateDiff = newDate.Subtract(oldDate); //using substraction method
     Console.WriteLine($"Date Difference is = {dateDiff.Days}");
  }
  
  static void main(string[] args)
  {
     DateTime date1 = new System.DateTime(2019, 3, 1, 12, 0, 0);
     DateTime date2 = new System.DateTime(2019, 3, 2, 13, 0, 0);
     DateDifference dateDifference = new DateDifference();
     dateDifference.getDateDiff(date1, date2); //output-> 1

   }
 }
In here we are using only the Days property of the TimeSpan structure. Nevertheless, there are many other properties like Hours, Minutes, Seconds, TotalDays etc.


Date Difference by Subtraction (-) Operator

The easier way of finding the date difference is by using the minus operator. In the below example, we’ll be creating a method called getDateDiff() and passes two DateTime arguments to it. From the passed arguments; Year, Minutes, Seconds etc. are extracted and passed to the constructor. Finally, getDateDiff() method is invoked in the main method using the values of the user. This method too results in the same output as the Subtract() method.
class DateDifference
{
  public void getDateDiff(DateTime date1, DateTime date2) {
     DateTime oldDate = new System.DateTime(date1.Year, date1.Month,
                          date1.Day, date1.Hour, date1.Minute, date1.Second);
     DateTime newDate = new System.DateTime(date2.Year, date2.Month,
                          date2.Day, date2.Hour, date2.Minute, date2.Second);
     TimeSpan dateDiff = newDate - oldDate; //using substraction operator
     Console.WriteLine($"Date Difference is = {dateDiff.Days}");
  }
  
  static void main(string[] args)
  {
     DateTime date1 = new System.DateTime(2019, 3, 1, 12, 0, 0);
     DateTime date2 = new System.DateTime(2019, 3, 2, 13, 0, 0);
     DateDifference dateDifference = new DateDifference();
     dateDifference.getDateDiff(date1, date2); //output-> 1

   }
 }


Apart from the previously stated 2 methods, VB.NET namespace provides DateDiff() function to calculate date difference between two dates. For that, we need to add the VB script to the C# project and it is not recommended to practice in C# environment. So far we have calculated the date difference using two methods, but one of the important points to be noticed is that, both the dates we used are considered to be in the same time zone. What happens if the dates are in different time zones? For that, we can format our final output as follows.

TimeSpan withTimezones = newDate.ToUniversalTime().Subtract(oldDate.ToUniversalTime());

By using ToUniversalTime() method, we are converting the two values into a common time zone before subtraction, so that no any other confusion occur during the conversion.

Monday, April 1, 2019

Over the Top Media and Its Evolution


Are you obsessed with OTT as well?

Over The Top media, more commonly known as OTT, refers to the delivery of audio, video and other media over the internet without the direct involvement of a telecommunication company. Although this term was originally limited to the delivery of audio and video, recently, it has expanded to cover any service that is available over the internet. A subscriber for OTT services is free to access his subscribed media from anywhere in the world at any time and eventually over any access technology.
The Future of OTT media: To Hope or Not to Hope?

The number of factors has sprung over the years that have created a favourable environment for the advancement of OTT media. On the one hand, the availability of the internet facility at an affordable cost has certainly played a major role in it. On the other hand, the deviation of many millennials from the traditional television schedule and their preference for selecting what and when to watch have certainly helped.

Moreover, since there are no cancellation costs, more people prefer these services.
The OTT the market is estimated to double by the end of 2019.

Apple,Amazon, Netflix, Hulu, Skype, Facebook, Dropbox, and Twitter are some of the OTT services provided by the leaders in the industry.

Why is it necessary to do market research?

Market research enables any service provider to recognize the unique challenges presented by the target clientele. The provider will also be able to identify information such as a would-be-clients opinion towards a free ad-supported service compared with an expensive service with no advertisements. It is also important to note that clients may have highly specific ideas about how they want to pay for the services they receive which would create more opportunities to service providers to monetize their service.
                      
Different Methods of Market Research for OTT
1.       Qualitative Research

This method of research is primarily used to gain an understanding of non-numerical data such as reasons, ideas, and opinions. This can also be used to identify the rising trends in opinions and thoughts. Researchers who use the qualitative method to analyze data use a wide array of techniques to find out how people become conscious of their social realities. Online ethnography, unstructured interviews, focus groups and online surveys with more open-ended questions are some of the examples for qualitative research methods.

Qualitative research is able to provide in-depth answers to what makes a consumer subscribe to a service. It also provides an insight into what makes a consumer subscribe to a particular service and reject another. Moreover, through qualitative research, a link can be formed to identify the actual thinking process behind the stated opinion of a consumer. Simply stated, this method allows a user to acquire rich, descriptive information from consumers.

2.       Quantitative Research

This method uses numerical, measurable data to develop facts and identify patterns. That data used for quantitative research is in the form of numbers and statistics often arranged in graphs, charts, and other non-textual forms. This research method includes a range of data-extracting techniques such as online polls, mobile and app surveys, kiosk surveys, systematic observations, etc.

An advantage of quantitative research is that the data collected through this method are reliable when it comes to acquiring data of a large population. Besides, the outcome of this research is relatively easy to measure compared with the qualitative method. A major advantage of quantitative research is the ability to easily convert its outcome into highly-accurate predictions.


3.       Desk Research

Also known as secondary research, desk research refers to an accumulation of data that already exist somewhere. These data may exist as published government reports, company records, newspaper, and magazine information or you can simply obtain them from the internet.
Desk Research is advantageous due to the availability of data at any time.  It is also freely accessible.

Issues faced by Competitors in the OTT Industry

Although the streaming is still dominated by Netflix, more and more content owners are publishing their content across various platforms. As the competition increases within this field, it would get more difficult with time to get noticed. Somehow, all the competitors want their apps on a home screen of a smartphone.
One way or the other, OTT has become a revolutionary giant in the field of entertainment.