2010-10-22 14 views
8

शुरू करें अरे दोस्तों, यह कैसे करना है इस पर थोड़ा सा खो गया है। मुझे पता है कि घोषणापत्र के समय मूल्यों के साथ सरणी को कैसे प्रारंभ किया जाए, लेकिन मैं डेटटाइम प्रकार सरणी के साथ ऐसा कैसे करूं क्योंकि यह दिनांक बनाने के लिए कई तर्क लेता है ??सी #: डेटटाइम सरणी

उत्तर

32

आपका मतलब यह है?

DateTime[] dateTimes = new DateTime[] 
{ 
    new DateTime(2010, 10, 1), 
    new DateTime(2010, 10, 2), 
    // etc 
}; 
+0

यह काफी आसान लग रहा है। नए कीवर्ड का उपयोग करने से समस्याएं पैदा नहीं हो रही हैं? – Sinaesthetic

+0

कोई याद नहीं है दिनांक दिनांक टाइम्स ऑब्जेक्ट्स की एक सरणी है, इसलिए इसके अंदर डेटटाइम क्लास का उदाहरण होना चाहिए। – Necronet

+0

ठीक है, मैंने इसे नए कीवर्ड के बिना आजमाया है। बस {डेटटाइम (एक्स, एक्स, एक्स)} आदि के साथ ठीक काम करने लगता है। मैं सिर्फ चिंतित था कि नया कीवर्ड प्रत्येक मान के लिए नई ऑब्जेक्ट्स तैयार करेगा, जिसकी मुझे आवश्यकता नहीं थी। धन्यवाद! – Sinaesthetic

5
DateTime [] startDate = new DateTime[5]; 
     startDate[0] = new DateTime(11, 11, 10); 
     startDate[1] = new DateTime(11, 11, 10); 
     startDate[2] = new DateTime(11, 11, 10); 
     startDate[3] = new DateTime(11, 11, 10); 
     startDate[4] = new DateTime(11, 11, 10); 
+2

अंतिम पंक्ति एक त्रुटि का कारण बन जाएगी, क्योंकि सरणी में केवल 5 तत्व हैं। – Matt

0
DateTime [] "name_of_array"=new Date[int lenght_of_the_array]; //this is the array DateTime 

और फिर जब आप सरणी के प्रत्येक स्थिति में मान असाइन:

DateTime "name_of_each_element_of_the_array"= new DateTime(int value_of_year,int value_of_month, int value_of_day);//this is each element that is added in each position of the array 
0
For example, i want to add a DateTime array of 4 elements:      DateTime[] myarray=new DateTime [4]; //the array is created 
int year, month, day;    //variables of date are created    
for(int i=0; i<myarray.length;i++) 
{ 
    Console.WriteLine("Day"); 
    day=Convert.ToInt32(Console.ReadLine()); 
    Console.WriteLine("Month"); 
    month=Convert.ToInt32(Console.ReadLine()); 
    Console.WriteLine("Year"); 
    year=Convert.ToInt32(Console.ReadLine()); 

    DateTime date =new DateTime(year,month,day); //here is created the object DateTime, that contains day, month and year of a date 

myarray[i]=date; //and then we set each date in each position of the array 
} 
+0

यह declaration_ के _time पर प्रारंभ नहीं होता है। – namezero

1

आप दो तिथियों के बीच समय अवधि के लिए एक सरणी का निर्माण करना चाहते हैं आप ऐसा कुछ कर सकते हैं:

 timeEndDate = timeStartDate.AddYears(1); // or .AddMonts etc.. 
     rangeTimeSpan = timeEndDate.Subtract(timeStartDate); //declared prior as TimeSpan object 
     rangeTimeArray = new DateTime[rangeTimeSpan.Days]; //declared prior as DateTime[] 

     for (int i = 0; i < rangeTimeSpan.Days; i++) 
     { 
      timeStartDate = timeStartDate.AddDays(1); 
      rangeTimeArray[i] = timeStartDate; 
     } 
संबंधित मुद्दे