Professional Applications Programmers/Consultants برمجة واستشارات تطبيقات الإنترنت
Skip Navigation LinksHome » Code Library » Hijri

Public general use code classes and xml files that we've compiled and used over the years:

Hijri date handler class.

   1:  using System;
   2:  using System.Web;
   3:  using System.Diagnostics;
   4:  using System.Globalization;
   5:  using System.Data;
   6:  using System.Collections;
   7:   
   8:  /*
   9:   * Attention
  10:   * This library has been written by: Anas Reslan Bahsas if you are going to use it please dont remove this line.
  11:   * you have to add this class to a asp.net web project to work well. I will be grateful to receive any commments or 
  12:   * suggestion to anasbahsas@hotmail.com
  13:   * 
  14:   * http://www.aawsat.com/
  15:   */
  16:   
  17:  namespace Ia.Cl.Model
  18:  {
  19:      ////////////////////////////////////////////////////////////////////////////
  20:   
  21:      /// <summary publish="true">
  22:      /// Hijri date handler class.
  23:      /// </summary>
  24:      /// <remarks> 
  25:      /// Copyright © 2001-2015 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  26:      ///
  27:      /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
  28:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  29:      ///
  30:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  31:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  32:      /// 
  33:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  34:      /// 
  35:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  36:      /// </remarks> 
  37:      public class Hijri
  38:      {
  39:          private static HttpContext cur;
  40:   
  41:          private const int startGreg = 1900;
  42:          private const int endGreg = 2100;
  43:          private static string[] allFormats = { "yyyy/MM/dd", "yyyy/M/d", "dd/MM/yyyy", "d/M/yyyy", "dd/M/yyyy", "d/MM/yyyy", "yyyy-MM-dd", "yyyy-M-d", "dd-MM-yyyy", "d-M-yyyy", "dd-M-yyyy", "d-MM-yyyy", "yyyy MM dd", "yyyy M d", "dd MM yyyy", "d M yyyy", "dd M yyyy", "d MM yyyy" };
  44:          private static CultureInfo arCul;
  45:          private static CultureInfo enCul;
  46:          private static HijriCalendar h;
  47:          private static GregorianCalendar g;
  48:   
  49:          ////////////////////////////////////////////////////////////////////////////
  50:   
  51:          /// <summary>
  52:          ///
  53:          /// </summary>
  54:          public Hijri()
  55:          {
  56:              cur = HttpContext.Current;
  57:   
  58:              arCul = new CultureInfo("ar-SA");
  59:              enCul = new CultureInfo("en-US");
  60:   
  61:              h = new HijriCalendar();
  62:              g = new GregorianCalendar(GregorianCalendarTypes.USEnglish);
  63:   
  64:              arCul.DateTimeFormat.Calendar = h;
  65:          }
  66:   
  67:          ////////////////////////////////////////////////////////////////////////////
  68:   
  69:          /// <summary>
  70:          /// Check if string is hijri date and then return true 
  71:          /// </summary>
  72:          /// <param name="hijri"></param>
  73:          /// <returns></returns>
  74:          public static bool IsHijri(string hijri)
  75:          {
  76:              if (hijri.Length <= 0)
  77:              {
  78:                  cur.Trace.Warn("IsHijri Error: Date String is Empty");
  79:                  return false;
  80:              }
  81:              try
  82:              {
  83:                  DateTime tempDate = DateTime.ParseExact(hijri, allFormats, arCul.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces);
  84:                  if (tempDate.Year >= startGreg && tempDate.Year <= endGreg) return true;
  85:                  else return false;
  86:              }
  87:              catch (Exception ex)
  88:              {
  89:                  cur.Trace.Warn("IsHijri Error :" + hijri.ToString() + "\n" + ex.Message);
  90:                  return false;
  91:              }
  92:          }
  93:   
  94:          ////////////////////////////////////////////////////////////////////////////
  95:   
  96:          /// <summary>
  97:          /// Check if string is Gregorian date and then return true 
  98:          /// </summary>
  99:          /// <param name="greg"></param>
 100:          /// <returns></returns>
 101:          public static bool IsGreg(string greg)
 102:          {
 103:              if (greg.Length <= 0)
 104:              {
 105:                  cur.Trace.Warn("IsGreg :Date String is Empty");
 106:                  return false;
 107:              }
 108:              try
 109:              {
 110:                  DateTime tempDate = DateTime.ParseExact(greg, allFormats, enCul.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces);
 111:                  if (tempDate.Year >= startGreg && tempDate.Year <= endGreg) return true;
 112:                  else return false;
 113:              }
 114:              catch (Exception ex)
 115:              {
 116:                  cur.Trace.Warn("IsGreg Error :" + greg.ToString() + "\n" + ex.Message);
 117:                  return false;
 118:              }
 119:          }
 120:   
 121:          ////////////////////////////////////////////////////////////////////////////
 122:   
 123:          /// <summary>
 124:          /// Return Formatted Hijri date string 
 125:          /// </summary>
 126:          /// <param name="date"></param>
 127:          /// <param name="format"></param>
 128:          /// <returns></returns>
 129:          public static string FormatHijri(string date, string format)
 130:          {
 131:              if (date.Length <= 0)
 132:              {
 133:                  cur.Trace.Warn("Format :Date String is Empty");
 134:                  return "";
 135:              }
 136:              try
 137:              {
 138:                  DateTime tempDate = DateTime.ParseExact(date, allFormats, arCul.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces);
 139:                  return tempDate.ToString(format, arCul.DateTimeFormat);
 140:   
 141:              }
 142:              catch (Exception ex)
 143:              {
 144:                  cur.Trace.Warn("Date :\n" + ex.Message);
 145:                  return "";
 146:              }
 147:          }
 148:   
 149:          ////////////////////////////////////////////////////////////////////////////
 150:   
 151:          /// <summary>
 152:          /// Returned Formatted Gregorian date string
 153:          /// </summary>
 154:          /// <param name="date"></param>
 155:          /// <param name="format"></param>
 156:          /// <returns></returns>
 157:          public static string FormatGreg(string date, string format)
 158:          {
 159:              if (date.Length <= 0)
 160:              {
 161:                  cur.Trace.Warn("Format :Date String is Empty");
 162:                  return "";
 163:              }
 164:              try
 165:              {
 166:                  DateTime tempDate = DateTime.ParseExact(date, allFormats, enCul.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces);
 167:                  return tempDate.ToString(format, enCul.DateTimeFormat);
 168:              }
 169:              catch (Exception ex)
 170:              {
 171:                  cur.Trace.Warn("Date :\n" + ex.Message);
 172:                  return "";
 173:              }
 174:          }
 175:   
 176:          ////////////////////////////////////////////////////////////////////////////
 177:   
 178:          /// <summary>
 179:          /// Return Today Gregorian date and return it in yyyy/MM/dd format
 180:          /// </summary>
 181:          /// <returns></returns>
 182:          public static string GDateNow()
 183:          {
 184:              try
 185:              {
 186:                  return DateTime.UtcNow.AddHours(3).ToString("yyyy-MM-dd", enCul.DateTimeFormat);
 187:              }
 188:              catch (Exception ex)
 189:              {
 190:                  cur.Trace.Warn("GDateNow :\n" + ex.Message);
 191:                  return "";
 192:              }
 193:          }
 194:   
 195:          ////////////////////////////////////////////////////////////////////////////
 196:   
 197:          /// <summary>
 198:          /// Return formatted today Gregorian date based on your format
 199:          /// </summary>
 200:          /// <param name="format"></param>
 201:          /// <returns></returns>
 202:          public static string GDateNow(string format)
 203:          {
 204:              try
 205:              {
 206:                  return DateTime.UtcNow.AddHours(3).ToString(format, enCul.DateTimeFormat);
 207:              }
 208:              catch (Exception ex)
 209:              {
 210:                  cur.Trace.Warn("GDateNow :\n" + ex.Message);
 211:                  return "";
 212:              }
 213:          }
 214:   
 215:          ////////////////////////////////////////////////////////////////////////////
 216:   
 217:          /// <summary>
 218:          /// Return Today Hijri date and return it in yyyy-MM-dd format
 219:          /// </summary>
 220:          /// <returns></returns>
 221:          public string HDateNow()
 222:          {
 223:              try
 224:              {
 225:                  return DateTime.UtcNow.AddHours(3).ToString("yyyy-MM-dd", arCul.DateTimeFormat);
 226:              }
 227:              catch (Exception ex)
 228:              {
 229:                  cur.Trace.Warn("HDateNow :\n" + ex.Message);
 230:                  return "";
 231:              }
 232:          }
 233:   
 234:          ////////////////////////////////////////////////////////////////////////////
 235:   
 236:          /// <summary>
 237:          /// Return formatted today hijri date based on your format
 238:          /// </summary>
 239:          /// <param name="format"></param>
 240:          /// <returns></returns>
 241:          public static string HDateNow(string format)
 242:          {
 243:              try
 244:              {
 245:                  return DateTime.UtcNow.AddHours(3).ToString(format, arCul.DateTimeFormat);
 246:              }
 247:              catch (Exception ex)
 248:              {
 249:                  cur.Trace.Warn("HDateNow :\n" + ex.Message);
 250:                  return "";
 251:              }
 252:          }
 253:   
 254:          ////////////////////////////////////////////////////////////////////////////
 255:   
 256:          /// <summary>
 257:          /// Convert Hijri Date to it's equivalent Gregorian Date
 258:          /// </summary>
 259:          /// <param name="hijri"></param>
 260:          /// <returns></returns>
 261:          public static string HijriToGreg(string hijri)
 262:          {
 263:              if (hijri.Length <= 0)
 264:              {
 265:                  cur.Trace.Warn("HijriToGreg :Date String is Empty");
 266:                  return "";
 267:              }
 268:              try
 269:              {
 270:                  DateTime tempDate = DateTime.ParseExact(hijri, allFormats, arCul.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces);
 271:                  return tempDate.ToString("yyyy-MM-dd", enCul.DateTimeFormat);
 272:              }
 273:              catch (Exception ex)
 274:              {
 275:                  cur.Trace.Warn("HijriToGreg :" + hijri.ToString() + "\n" + ex.Message);
 276:                  return "";
 277:              }
 278:          }
 279:   
 280:          ////////////////////////////////////////////////////////////////////////////
 281:   
 282:          /// <summary>
 283:          /// Convert Hijri Date to it's equivalent Gregorian Date
 284:          /// and return it in specified format
 285:          /// </summary>
 286:          /// <param name="hijri"></param>
 287:          /// <param name="format"></param>
 288:          /// <returns></returns>
 289:          public static string HijriToGreg(string hijri, string format)
 290:          {
 291:              if (hijri.Length <= 0)
 292:              {
 293:                  cur.Trace.Warn("HijriToGreg :Date String is Empty");
 294:                  return "";
 295:              }
 296:              try
 297:              {
 298:                  DateTime tempDate = DateTime.ParseExact(hijri, allFormats, arCul.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces);
 299:                  return tempDate.ToString(format, enCul.DateTimeFormat);
 300:   
 301:              }
 302:              catch (Exception ex)
 303:              {
 304:                  cur.Trace.Warn("HijriToGreg :" + hijri.ToString() + "\n" + ex.Message);
 305:                  return "";
 306:              }
 307:          }
 308:   
 309:          ////////////////////////////////////////////////////////////////////////////
 310:   
 311:          /// <summary>
 312:          /// Convert Gregoian Date to it's equivalent Hijir Date
 313:          /// </summary>
 314:          /// <param name="greg"></param>
 315:          /// <returns></returns>
 316:          public static string GregToHijri(string greg)
 317:          {
 318:              if (greg.Length <= 0)
 319:              {
 320:                  cur.Trace.Warn("GregToHijri :Date String is Empty");
 321:                  return "";
 322:              }
 323:              try
 324:              {
 325:                  DateTime tempDate = DateTime.ParseExact(greg, allFormats, enCul.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces);
 326:                  return tempDate.ToString("yyyy-MM-dd", arCul.DateTimeFormat);
 327:              }
 328:              catch (Exception ex)
 329:              {
 330:                  cur.Trace.Warn("GregToHijri :" + greg.ToString() + "\n" + ex.Message);
 331:                  return "";
 332:              }
 333:          }
 334:   
 335:          ////////////////////////////////////////////////////////////////////////////
 336:   
 337:          /// <summary>
 338:          /// Convert Hijri Date to it's equivalent Gregorian Date and
 339:          /// return it in specified format
 340:          /// </summary>
 341:          /// <param name="greg"></param>
 342:          /// <param name="format"></param>
 343:          /// <returns></returns>
 344:          public static string GregToHijri(string greg, string format)
 345:          {
 346:              if (greg.Length <= 0)
 347:              {
 348:                  cur.Trace.Warn("GregToHijri :Date String is Empty");
 349:                  return "";
 350:              }
 351:              try
 352:              {
 353:                  DateTime tempDate = DateTime.ParseExact(greg, allFormats, enCul.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces);
 354:                  return tempDate.ToString(format, arCul.DateTimeFormat);
 355:              }
 356:              catch (Exception ex)
 357:              {
 358:                  cur.Trace.Warn("GregToHijri :" + greg.ToString() + "\n" + ex.Message);
 359:                  return "";
 360:              }
 361:          }
 362:   
 363:          ////////////////////////////////////////////////////////////////////////////
 364:   
 365:          /// <summary>
 366:          /// Return Gregrian Date Time as digit stamp
 367:          /// </summary>
 368:          /// <returns></returns>
 369:          public static string GTimeStamp()
 370:          {
 371:              return GDateNow("yyyyMMddHHmmss");
 372:          }
 373:   
 374:          ////////////////////////////////////////////////////////////////////////////
 375:          /// <summary>
 376:          /// Return Hijri Date Time as digit stamp
 377:          /// </summary>
 378:          /// <returns></returns>
 379:          public static string HTimeStamp()
 380:          {
 381:              return HDateNow("yyyyMMddHHmmss");
 382:          }
 383:   
 384:          ////////////////////////////////////////////////////////////////////////////
 385:   
 386:          /// <summary>
 387:          /// Compare two instaces of string date 
 388:          /// and return indication of thier values 
 389:          /// </summary>
 390:          /// <param name="d1"></param>
 391:          /// <param name="d2"></param>
 392:          /// <returns>positive d1 is greater than d2,
 393:          /// negative d1 is smaller than d2, 0 both are equal</returns>
 394:          public static int Compare(string d1, string d2)
 395:          {
 396:              try
 397:              {
 398:                  DateTime date1 = DateTime.ParseExact(d1, allFormats, arCul.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces);
 399:                  DateTime date2 = DateTime.ParseExact(d2, allFormats, arCul.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces);
 400:                  return DateTime.Compare(date1, date2);
 401:              }
 402:              catch (Exception ex)
 403:              {
 404:                  cur.Trace.Warn("Compare :" + "\n" + ex.Message);
 405:                  return -1;
 406:              }
 407:          }
 408:   
 409:          ////////////////////////////////////////////////////////////////////////////
 410:          ////////////////////////////////////////////////////////////////////////////
 411:      }
 412:  }
 413: