Showing posts with label DateTime. Show all posts
Showing posts with label DateTime. Show all posts

Jul 12, 2012

Reset Time of CrmDateTime

Surprisingly, I couldn’t find straightforward post of how to reset the time portion of the CrmDateTime. So I implemented my own method which worked for me. In fact, this might not the best code, but decent enough to use.

In my previous post I gave a hint that “Value” attribute is the key to CrmDateTime modifications/conversions. See, how I have used same attribute to modify the Time portion only.

public static CrmDateTime SetTime(CrmDateTime _dateTime,string _time)
{
  // considering datetime format as 2012-07-05T11:00:00+1000
  string _crmDTvalue = Convert.ToString(_dateTime.Value);
  string _crmDTvalueP1 = _crmDTvalue.Substring(0, 11);
  string _crmDTvalueP2 = _crmDTvalue.Substring(16);
  string _crmDTvalueNewTime = string.Concat(_crmDTvalueP1, _time, _crmDTvalueP2);

  CrmDateTime _crmDataTime = new CrmDateTime();
  _crmDataTime.Value = _crmDTvalueNewTime;

  return _crmDataTime;
}

Calling part;

//8 am
SetTime(_myCrmDateTime,"08:00");
//5 pm
SetTime(_myCrmDateTime,"17:00");

Please keep in mind, this method is subjective to the date time format you use, but this approach is easily used for any format.

CrmDateTime to/from DateTime

This is a simple kind of conversion, but for some reason, I had to play around to figure it out. So I am writing it here.

When converting to/from CrmDateTime in C# key is the “Value” attribute. We can simply read it and also assign it.

CrmDateTime to DateTime

DateTime _datetime = Convert.ToDateTime(dateTime.Value)

DateTime to CrmDateTime

 CrmDateTime _crmDataTime = new CrmDateTime();
 _crmDataTime.Value = _datetime.ToString("s");

Also, we can modify date time related other attributes this way.

Mar 4, 2012

Calculate business days in c# (with variable business days per week)

One of my past posts (i.e. here) I discussed calculating hire days in JavaScrips for equipments (including start and end days) with variable number of days per week. It could be 5 as a default, but one can work on Saturdays too and also there can be non-stop operations which we should count 7 days per week. Here is the appropriate c# code.

public static int getHireDays(DateTime startDate, DateTime endDate, int NoOfDays)
        {
        int _saturdays = 0;
        int _sundays = 0;
        int _Bdays = 0;

        endDate += TimeSpan.FromDays(1); //add one beacuse we need days INCLUDING end date
        long ticks = (endDate.Ticks - startDate.Ticks) / 10000000; //seconds
        int days = (int)(ticks / 86400);

        endDate -= TimeSpan.FromDays(1);
        for (DateTime i = startDate; i < endDate; i += TimeSpan.FromDays(1))
        {
            if (i.DayOfWeek == DayOfWeek.Saturday) { _saturdays = _saturdays + 1; }
            if (i.DayOfWeek == DayOfWeek.Sunday) { _sundays = _sundays + 1; }
        }

        switch (NoOfDays)
        {
            case 5:
                _Bdays = days - (_sundays + _saturdays);
                break;
            case 6:
                _Bdays = days - _saturdays;
                break;
            case 7:
                _Bdays = days;
                break;
        }
        return _Bdays;
        }

Since I am doing a loop here, this can be bit inappropriate for long periods, but I haven’t tested the performances for long periods.

Sep 13, 2011

DateFormats and Ascentium CrmService

Playing around with DateTimes seems quite hectic to me. Specially, when you work in client side with Ascentium CrmService, it becomes tricky since you need to do your operations in usual DateTime format available in JavaScript and convert them from/to methods in the service.

For example, if you need some DateTime operation done in your logic using JavaScript, for some value comes from a Fetch result, first you need to convert them to a DateTime field. Fetch returns strings. There is no simple way for that but to do it with some string operations as below.

//convert DATE String (2008-01-01T00:00:00) format 
//to DATE (Tue Aug 23 07:00:00 UTC+1000 2011)
DateFormatFromAttributeValue = function(DateStr)
{
    var yearStr, YearStr, DayStr, MonthStr, HourStr, MinStr, NewDateStr;
    yearStr = DateStr.substr(0, 4);
    MonthStr = String(DateStr.substr(5, 2) - 1); //Month goes as 0-11
    DayStr = DateStr.substr(8, 2);
    HourStr = DateStr.substr(11, 2);
    MinStr = DateStr.substr(14, 2);

    return new Date(yearStr, MonthStr, DayStr, HourStr, MinStr, 0);
}

It is interesting to see that you get three string values from the Fetch as below.


My advice is to use “value” field for all your operations even if you need just the date. I am telling this because, date string is not consistent with the length which could drag you to a mess when handling. If you see carefully, in above example, date is shown as “2” not “02”. In fact, number of characters in the string could be different when date is less than 10th day and greater than 10th Day. This should be a big concern when string functions are being used for conversions.

When you need to submit a DateTime, you got to convert it back to the previous string format (i.e. 2008-01-01T00:00:00). I use below method for that.

//convert DATE (Tue Aug 23 07:00:00 UTC+1000 2011) 
//to DATE String (2008-01-01T00:00:00) format
DateFormatPassToService = function(DateValue)
{
  var YearStr, DayStr, MonthStr, HourStr, MinStr, NewDateStr;
  YearStr = DateValue.getFullYear();
  MonthStr = String(parseInt(DateValue.getMonth()) + 1); //Month goes as 0-11
      if (MonthStr.length == 1) { MonthStr = '0' + MonthStr; }
  DayStr = String(DateValue.getDate());
      if (DayStr.length == 1) { DayStr = '0' + DayStr; }
  HourStr = String(DateValue.getHours());
      if (HourStr.length == 1) { HourStr = '0' + HourStr; }
  MinStr = String(DateValue.getMinutes());
      if (MinStr.length == 1) { MinStr = '0' + MinStr; }
  return (YearStr + '-' + MonthStr + '-' + DayStr + 'T' + HourStr + ':' + MinStr + ':00');
}


Hope this will help you.

Sep 12, 2011

Calculate business days in JavaScript (with variable business days per week)

There are lot of ways to calculate working days. Anyway, it was not easy to find a method to calculate business days if the number of working days per week is being changed. By default, working days will be Monday to Friday, but in some cases you might need to calculate days including Saturday or both Saturday and Sunday.

In my case it was required to calculate the cost of hire when renting equipments. They are to be charged according to the days they are being used. Some factories could work just 5 days while some work for 6 or 7 days per week.

I couldn’t find any method in the web to do it in client side. Below is the only way I could think of. Actually hint was given by one of the colleagues.

calcBusinessDaysExtended = function (Start, End, DaysPerWeek)
{
    Start.setHours(0);
    End.setHours(0);

    _StartUTCmilli = Date.UTC(Start.getFullYear(), Start.getMonth(), Start.getDate());
    _EndUTCmilli = Date.UTC(End.getFullYear(), End.getMonth(), End.getDate());
    var _Days = Math.floor((_EndUTCmilli - _StartUTCmilli) / 86400000) + 1;

    var _Saturdays = 0;
    var _Sundays = 0;
    var _bdays = 0;

    var nextDatOfEnd = new Date(End.setDate(End.getDate() + 1));
    for (i = Start; i < nextDatOfEnd; i.setDate(i.getDate() + 1))
    {
        if (i.getDay() == 6) { _Saturdays = _Saturdays + 1; }
        if (i.getDay() == 0) { _Sundays = _Sundays + 1; }
    }

    switch (DaysPerWeek)
    {
        case 5:
            _bdays = _Days - (_Saturdays + _Sundays);
            break;
        case 6:
            _bdays = _Days - _Sundays;
            break;
        default:
            _bdays = _Days;
    }
    return _bdays;
}

One can think, loops could affect the efficiency, but it is not really matters in client side work. I am happy if someone can provide me a better one.. Please find the relevant c# code here.