DateTime Format in C#: C# में DateTime व Time को सामान्यत Data Type की तरह Use करने के लिए System.DateTime व System.TimeSpan नाम की दो Special Structures को Define किया गया है। इन Structures के Objects के साथ हम विभिन्न प्रकार के Predefined Methods, Properties व Overloaded Operators का प्रयोग करके Date व Time को Primary Data Type की तरह Use कर सकते हैं।
DateTime Format in C#: System.DateTime and System.TimeSpan
.NET Framework में System.DateTime व System.TimeSpan नाम की जिन दो Special Types को Define किया गया है, उन्हें Use करने के लिए C# हमें कोई Shorthand Notation Provide नहीं करता।
DateTime Type का प्रयोग Specific Date (Month, Day, Year) Time Data को Represent करने के लिए Use किया जाता है। जबकि हम Date व Time को विभिन्न तरीकों से Format करते हुए Manipulate कर सकते हैं।
DateTime, TimeSpan, System.Void व System.Guid वास्तव में Classes नहीं बल्कि C/C++ Programming Language में बना, जाने वाले Structures हैं और इनमें बहुत सारे Methods व Properties को Define किया गया है, जिन्हे हम हमारी जरूरत के अनुसार Use कर सकते हैं।
DateTime व TimeSpan Structure में Date व Time Manipulate करने से सम्बंधित विभिन्न प्रकार की Properties व Methods के साथ सभी Relational व Unary Plus तथा Unary Minus Operators को भी Overload किया गया है, ताकि हम किसी भी Date, Time या DateTime Value के साथ ठीक उसी तरह से Arithmetical व Relational Operations Perform कर सकें, जिस तरह से Primary Data Types के साथ करते हैं।
इन सभी Types के बारे में Detailed जानकारी प्राप्त करने के लिए Visual Studio के Object Browser को Use किया जा सकता है। फिर भी DateTime Structure में Specified विभिन्न DateTime Related Properties को निम्नानुसार Program द्वारा आसानी से समझा जा सकता है:
// File Name: SystemTypeDateTimeProperties.cs using System; namespace CSharpFundamentals { class SystemTypeDateTimeProperties { private static void Main(string[] args) { DateTime today = DateTime.Now; Console.WriteLine("Date Related Properties"); Console.WriteLine("Today is: {0}", DateTime.Today); Console.WriteLine("Date is: {0}", today.Date); Console.WriteLine("Day is: {0}", today.Day); Console.WriteLine("Month is: {0}", today.Month); Console.WriteLine("Year is: {0}\n", today.Year); Console.WriteLine("Time Related Properties"); Console.WriteLine("Hour is: {0}", today.Hour); Console.WriteLine("Minute is: {0}", today.Minute); Console.WriteLine("Second is: {0}", today.Second); Console.WriteLine("Millisecond is: {0}\n", today.Millisecond); Console.WriteLine("Kind of Date related Property"); Console.WriteLine("Kind is: {0}\n", today.Kind); Console.WriteLine("Current Time related Properties"); Console.WriteLine("Now is: {0}", DateTime.Now); Console.WriteLine("UtcNow is: {0}\n", DateTime.UtcNow); Console.WriteLine("Start of Time Range is: {0}", DateTime.MinValue); Console.WriteLine("End of Time Range is: {0}\n", DateTime.MaxValue); Console.WriteLine("Date related other Properties"); Console.WriteLine("Day of Week is: {0}", today.DayOfWeek); Console.WriteLine("Day of Year is: {0}\n", today.DayOfYear); Console.WriteLine("Ticks related Properties"); Console.WriteLine("Ticks till Current Time is: {0}", today.Ticks); Console.WriteLine("Minimum Ticks till Current Time is: {0}", DateTime.MinValue.Ticks); Console.WriteLine("Maximum Ticks till Current Time is: {0}", DateTime.MaxValue.Ticks); } } } // Output: Date Related Properties Today is: 6/18/2013 12:00:00 AM Date is: 6/18/2013 12:00:00 AM Day is: 18 Month is: 6 Year is: 2013 Time Related Properties Hour is: 13 Minute is: 1 Second is: 57 Millisecond is: 654 Kind of Date related Property Kind is: Local Current Time related Properties Now is: 6/18/2013 1:01:57 PM UtcNow is: 6/18/2013 7:31:57 AM Start of Time Range is: 1/1/0001 12:00:00 AM End of Time Range is: 12/31/9999 11:59:59 PM Date related other Properties Day of Week is: Tuesday Day of Year is: 169 Ticks related Properties Ticks till Current Time is: 635071573176546142 Minimum Ticks till Current Time is: 0 Maximum Ticks till Current Time is: 3155378975999999999
इस Program में सबसे पहले हमने निम्न Statement द्वारा DateTime Type का एक नया Object Create किया है:
DateTime today = DateTime.Now;
इस Statement में हमने DateTime.Now नाम की Property को Specify किया है। ये Property एक Static Property है और Static Property को हमेंशा Type के Name के साथ Specify किया जाता है न कि Type के Object के साथ।
ये Statement DateTime Type का today नाम का एक नया Object Create करता है और DateTime.Now Statement द्वारा Current Time को इस today नाम के Object में Initialize कर देता है।
परिणामस्वरूप जब ये Statement Execute होता है, तो हमारे Computer का Current Time today नाम के Object में Initialize हो जाता है, जिस पर Program के अन्य Statements Execute होते हैं।
DateTime Structure की Date व Today दोनों Properties आज की Date Return करते हैं। लेकिन Today एक Static Property जिसे हमेंशा Dot Operator का प्रयोग करते हुए DateTime Class के साथ Use किया जाता है। जबकि Date एक Member Property है, जिसे हमेंशा DateTime Type के किसी Object के साथ Dot Operator का प्रयोग करते हुए Use किया जाता है।
जबकि किसी Specified Date के विभिन्न हिस्सों को Day, Month व Year Properties द्वारा Access किया जा सकता है तथा Date के साथ Associated Time के विभिन्न हिस्सों को Hour, Minute, Second व Milliseconds Properties द्वारा Access किया जा सकता है।
C# में DateTime को Local व UTC दो तरीकों से Specify किया जा सकता है। इसलिए Kind Property का प्रयोग करके हम इस बात का पता लगा सकते हैं कि Specified DateTime, Local DateTime है या UTC DateTime है।
Now व UtcNow Properties को भी Static Properties होने की वजह से DateTime Type के साथ Dot Operator का प्रयोग करके Use किए जाते हैं। साथ ही Now, Local Computer के Time को Represent करता है। जबकि UtcNow Universal Time Coordinate Time को Represent करता है।
MinValue व MaxValue भी दो Static Properties हैं, जो कि C# द्वारा Represent की जा सकने वाली DateTime Range को Represent करता है। यानी C# में हम 1/1/0001 12:00:00 AM से 12/31/9999 11:59:59 PM के बीच की किसी भी DateTime को Represent कर सकते हैं।
DayOfWeek Property, Specified Date के वार को Represent करता है जबकि DayOfYear Property, साल की शु:आत यानी 1 January से लेकर Specified Date तक के कुल बीते हुए दिनों की संख्या Return करता है।
Computer में DateTime को हमेंशा Processor के Ticks के आधार पर Calculate किया जाता है। इसलिए Ticks Property का प्रयोग करके हम इस बात का पता लगा सकते है कि शु:आत से लेकर Specified DateTime तक कुल कितने Ticks हो चुके हैं।
जबकि C# द्वारा Handle किए जा सकने वाले कुल Ticks की Minimum व Maximum संख्या को हम DateTime Structure के MinValue.Ticks व MaxValue.Ticks द्वारा प्राप्त कर सकते हैं।
इन Properties के अलावा कुछ Methods को भी Define किया गया है, जिनका प्रयोग DateTime Type के Objects के साथ DateTime Related Calculations को Perform करने के लिए किया जा सकता है। इन Methods को हम निम्नानुसार तरीके से Use कर सकते हैं:
// File Name: SystemTypeDateTimeMethods.cs using System; namespace CSharpFundamentals { class SystemTypeDateTimeMethods { private static void Main(string[] args) { DateTime today = DateTime.Now; Console.WriteLine("Same Date of Next Year is: {0}", today.AddYears(1)); Console.WriteLine("Date of Tomorrow is: {0}\n", today.AddDays(1)); Console.WriteLine("Time after 1 Hour would be : {0}", today.AddHours(1)); Console.WriteLine("Time after 10 Minutes would be : {0}", today.AddMinutes(10)); Console.WriteLine("Time after 50 Seconds would be : {0}", today.AddSeconds(50)); Console.WriteLine("Time after 10 Milliseconds would be:{0}",today.AddMilliseconds(10)); TimeSpan schedule = new TimeSpan(1, 20, 0); Console.WriteLine("Time after 1 Hour 20 Minutes would be : {0}", today.Add(schedule)); } } } // Output: Same Date of Next Year is: 6/18/2014 2:04:23 PM Date of Tomorrow is: 6/19/2013 2:04:23 PM Time after 1 Hour would be : 6/18/2013 3:04:23 PM Time after 10 Minutes would be : 6/18/2013 2:14:23 PM Time after 50 Seconds would be : 6/18/2013 2:05:13 PM Time after 100 Milliseconds would be : 6/18/2013 2:04:23 PM Time after 1 Hour 20 Minutes would be : 6/18/2013 3:24:23 PM
जैसाकि उपरोक्त Program व उसके Output द्वारा हम समझ सकते हैं कि हम Add***() Methods का प्रयोग करके एक DateTime Object में Argument के रूप में Specified मान के बराबर Year, Day, Hour, Minute, Second व Milliseconds जोडकर Resulted Output प्राप्त कर सकते हैं।
इसी तरह से और भी कई Methods Define किए गए हैं, जिनका प्रयोग करके हम Dates के साथ विभिन्न प्रकार की Processing कर सकते हैं। साथ ही हम इसी प्रकार से TimeSpan Type के Object को Use करते हुए Time के साथ भी विभिन्न प्रकार की Processing कर सकते हैं।
जिस तरह से हम सामान्य Primary Data Types के साथ विभिन्न प्रकार के Operators का प्रयोग करके विभिन्न प्रकार के Operations Perform करते हैं, उसी तरह से Addition व Subtraction जैसे Operations के साथ विभिन्न प्रकार के Comparision Operations Perform करने के लिए भी DateTime व TimeSpan Class में Binary Plus व Minus के साथ सभी Relational Operators को भी Overload किया गया है, जिन्हें हम किसी DateTime व TimeSpan Type के Object के साथ निम्न Program के अनुसार Use कर सकते हैं:
// File Name: SystemTypeDateTimeUsingOverloadedOperator.cs using System; namespace CSharpFundamentals { class SystemTypeDateTimeUsingOverloadedOperator { private static void Main(string[] args) { DateTime today = new DateTime(2001, 1, 1); DateTime before = new DateTime(2000, 1, 1); TimeSpan time = new TimeSpan(5, 10, 0); Console.WriteLine("today - before : {0} days.", today - before); Console.WriteLine("today + time: {0}\n", today + time); Console.WriteLine("today < before : {0}", today < before); Console.WriteLine("today > before : {0}", today > before); Console.WriteLine("today <= before : {0}", today <= before); Console.WriteLine("today >= before : {0}", today >= before); Console.WriteLine("today != before : {0}", today != before); Console.WriteLine("today == before : {0}", today == before); } } } // Output: today - before : 366.00:00:00 days. today + time: 1/1/2001 5:10:00 AM today < before : False today > before : True today <= before : False today >= before : True today != before : True today == before : False
यदि आपको OOPS Concepts व C++/Java Programming का पर्याप्त ज्ञान है, तो आप बडी ही आसानी से Visual Studio के Object Browser का प्रयोग करते हुए .NET Framework के विभिन्न Types की Detailed Study कर सकते हैं।
System.Numerics.dll Assembly
.NET Framework में System.Numerics Assembly में BigInteger व Complex नाम के दो Structures को Define किया गया है, जिनका प्रयोग बहुत बडी Integer संख्या व Complex Numbers को Manipulate करने के लिए किया जाता है। BigInteger Type के Object का प्रयोग करके हम ऐसी संख्याओं को Manipulate करने की क्षमता प्राप्त कर लेते हैं, जिनकी Lowest व Highest Limit का पता नहीं होता।
जब हम BigInteger Type का Object Create करना चाहते हैं, तो हमें हमारे C# Program में System.Numerics Namespace को using Statement का प्रयोग करते हुए Specify करना जरूरी होता है। यदि हम इसे बिना Specify किए हुए अपना C# Program Create करें, तो C# हमें निम्नानुसार Error Return करता है:
SystemTypeDateBigInteger.cs(9,4): error CS0246: The type or namespace name ‘BigInteger’ could not be found (are you missing a using directive or an assembly reference?)
इसी तरह से जब हम इस Program को Compile करते है, तब Compile करते समय भी हमें System.Numerics.dll Assembly को Parameter के रूप में Specify करना जरूरी होता है, क्योंकि इस Assembly को .NET Framework की csc.rsp File में Specify नहीं किया गया है।
// File Name: SystemTypeDateBigInteger.cs using System; using System.Numerics; namespace CSharpFundamentals { class SystemTypeDateBigInteger { private static void Main(string[] args) { BigInteger bigInt = BigInteger.Parse("99999999999999999999999999999999999999999"); Console.WriteLine("Value of bigInt {0}", bigInt); } } } // Output: Value of bigInt 99999999999999999999999999999999999999999
जैसाकि इस Program के Output में हम देख सकते हैं कि हमने एक बहुत ही बडी संख्या को bigInt नाम के Object में Store किया है और संख्या को Store करने के लिए उसे एक String Format में Specify करते हुए Parse() Method का प्रयोग करके bigInt नाम के Variable को Initialize किया है।
क्योंकि बिना Parse() Method का प्रयोग किए, यदि हम केवल BigInteger() Constructor का प्रयोग करते हुए इतनी बडी संख्या से bigInt को Initialize करने की कोशिश करते, तो C# हमें Compile Time Error देता। क्योंकि Long Integer से बडी संख्या को Constructor का प्रयोग करके Initialize नहीं कर सकते, न ही इतनी बडी संख्या को String के रूप में bigInt Variable को Initialize कर सकते हैं।
BigInteger Type के Object के साथ विभिन्न प्रकार के Calculations Perform करने के लिए इसी Structure में कई Methods को Define किया गया है। साथ ही Numerical Values के साथ काम करने वाले लगभग सभी Operators को Overload किया गया है, ताकि इन सभी Operators को BigInteger Type के Variables के साथ विभिन्न प्रकार के Operations को Exactly उसी प्रकार से Perform किया जा सके, जिस तरह से अन्य Data Types के साथ किया जाता है।
इसी प्रकार से सामान्यत: उपयोग में आने वाले Mathematical Methods व Properties को System.Complex Structure में Define किया गया है, जिनका प्रयोग विभिन्न प्रकार के Complex Type के Object के साथ किया जा सकता है।
System.Numerics Namespace में Specified दोनों ही Structures की विभिन्न Properties, Methods व Overloaded Operators की Detailed जानकारी के लिए Visual Studio के Object Browser Utility का प्रयोग किया जा सकता है।
ये Article इस वेबसाईट पर Selling हेतु उपलब्ध EBook C#.NET in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी रहा, तो निश्चित रूप से ये पुस्तक भी आपके लिए काफी उपयोगी साबित होगी।
C#.NET in Hindi | Page:908 | Format: PDF