PDA

View Full Version : نحوه تعریف نوشتن کلاس تاریخ و زمان



bitcob589
یک شنبه 21 اسفند 1390, 11:32 صبح
با سلام
نحوه نوشتن کلاس تاریخ و زمان به چه صورتی است

bitcob589
یک شنبه 21 اسفند 1390, 12:09 عصر
نحوه تعریف یک کلاس برای تاریخ شبیه زیر

3/2/2012 12:05:07:00 am]

bitcob589
یک شنبه 21 اسفند 1390, 14:52 عصر
کلاس به صورت زیر نوشته شده است ولی خطا می دهد برای تاریخی شبیه پست قبل

private Datetime =datetimestart
public barnamhnevis
{
datetimestart=("{0}{1}{2}{3}");
}
لطفا راهنمایی بفرمایید

alonemm
یک شنبه 21 اسفند 1390, 15:10 عصر
باسلام:
به مثال زیر دقت کنید:

using System;

class DateTimeTester
{
static bool RoughlyEquals(DateTime time, DateTime timeWithWindow, int windowInSeconds, int frequencyInSeconds)
{
long delta = (long)((TimeSpan)(timeWithWindow - time)).TotalSeconds
% frequencyInSeconds;

delta = delta > windowInSeconds ? frequencyInSeconds - delta : delta;

return Math.Abs(delta) < windowInSeconds;
}

public static void Main()
{
int window = 10;
int freq = 60 * 60 * 2; // 2 hours;

DateTime d1 = DateTime.Now;

DateTime d2 = d1.AddSeconds(2 * window);
DateTime d3 = d1.AddSeconds(-2 * window);
DateTime d4 = d1.AddSeconds(window / 2);
DateTime d5 = d1.AddSeconds(-window / 2);

DateTime d6 = (d1.AddHours(2)).AddSeconds(2 * window);
DateTime d7 = (d1.AddHours(2)).AddSeconds(-2 * window);
DateTime d8 = (d1.AddHours(2)).AddSeconds(window / 2);
DateTime d9 = (d1.AddHours(2)).AddSeconds(-window / 2);

Console.WriteLine("d1 ({0}) ~= d1 ({1}): {2}",
d1, d1, RoughlyEquals(d1, d1, window, freq));
Console.WriteLine("d1 ({0}) ~= d2 ({1}): {2}",
d1, d2, RoughlyEquals(d1, d2, window, freq));
Console.WriteLine("d1 ({0}) ~= d3 ({1}): {2}",
d1, d3, RoughlyEquals(d1, d3, window, freq));
Console.WriteLine("d1 ({0}) ~= d4 ({1}): {2}",
d1, d4, RoughlyEquals(d1, d4, window, freq));
Console.WriteLine("d1 ({0}) ~= d5 ({1}): {2}",
d1, d5, RoughlyEquals(d1, d5, window, freq));

Console.WriteLine("d1 ({0}) ~= d6 ({1}): {2}",
d1, d6, RoughlyEquals(d1, d6, window, freq));
Console.WriteLine("d1 ({0}) ~= d7 ({1}): {2}",
d1, d7, RoughlyEquals(d1, d7, window, freq));
Console.WriteLine("d1 ({0}) ~= d8 ({1}): {2}",
d1, d8, RoughlyEquals(d1, d8, window, freq));
Console.WriteLine("d1 ({0}) ~= d9 ({1}): {2}",
d1, d9, RoughlyEquals(d1, d9, window, freq));
}
}
// The example displays output similar to the following:
// d1 (1/28/2010 9:01:26 PM) ~= d1 (1/28/2010 9:01:26 PM): True
// d1 (1/28/2010 9:01:26 PM) ~= d2 (1/28/2010 9:01:46 PM): False
// d1 (1/28/2010 9:01:26 PM) ~= d3 (1/28/2010 9:01:06 PM): False
// d1 (1/28/2010 9:01:26 PM) ~= d4 (1/28/2010 9:01:31 PM): True
// d1 (1/28/2010 9:01:26 PM) ~= d5 (1/28/2010 9:01:21 PM): True
// d1 (1/28/2010 9:01:26 PM) ~= d6 (1/28/2010 11:01:46 PM): False
// d1 (1/28/2010 9:01:26 PM) ~= d7 (1/28/2010 11:01:06 PM): False
// d1 (1/28/2010 9:01:26 PM) ~= d8 (1/28/2010 11:01:31 PM): True
// d1 (1/28/2010 9:01:26 PM) ~= d9 (1/28/2010 11:01:21 PM): True

bitcob589
یک شنبه 21 اسفند 1390, 15:24 عصر
با تشکر از شما
کاربر تاریخ را تکس باکس به صورت شمسی وارد می کند و در دیتابیس به صورت قمری ذخیره می شود
یک کلاس نیاز است تا فرمت ذخیره شدن تاریخ و زمان را مشخص کند

alonemm
یک شنبه 21 اسفند 1390, 17:27 عصر
باسلام:
برای تبدیل تاریخ میتونید از PersianCalendar استفاده کنید:

به مثال زیر توجه کنید:


// This example demonstrates the members of the PersianCalendar class.

using System;
using System.Globalization;

class Sample
{
public static void Main()
{
//--------------------------------------------------------------------------------
// Get today's date.
//--------------------------------------------------------------------------------
Console.WriteLine("\n................. Today ...........................\n");
PersianCalendar jc = new PersianCalendar();
DateTime thisDate = DateTime.Now;

// Display the current date using the Gregorian and Persian calendars.
Console.WriteLine("Today is:");
Console.WriteLine(" {0:dddd}, {0} in the Gregorian calendar.", thisDate);
Console.WriteLine(" {0}, {1}/{2}/{3} {4}:{5}:{6} in the Persian calendar.",
jc.GetDayOfWeek(thisDate),
jc.GetMonth(thisDate),
jc.GetDayOfMonth(thisDate),
jc.GetYear(thisDate),
jc.GetHour(thisDate),
jc.GetMinute(thisDate),
jc.GetSecond(thisDate));
//--------------------------------------------------------------------------------
// Fields
//--------------------------------------------------------------------------------
Console.WriteLine("\n............... Fields .............................\n");
Console.WriteLine("PersianEra = {0}", PersianCalendar.PersianEra);
//--------------------------------------------------------------------------------
// Properties
//--------------------------------------------------------------------------------
Console.WriteLine("\n............... Properties .........................\n");
Console.Write("Eras:");
foreach (int era in jc.Eras)
{
Console.WriteLine(" era = {0}", era);
}
//--------------------------------------------------------------------------------
Console.WriteLine("\nGregorian Date Range Supported by the Persian Calendar:");
Console.WriteLine(" From {0:G}", jc.MinSupportedDateTime);
Console.WriteLine(" To {0:G}", jc.MaxSupportedDateTime);
//--------------------------------------------------------------------------------
Console.WriteLine("\nTwoDigitYearMax = {0}", jc.TwoDigitYearMax);
//--------------------------------------------------------------------------------
// Methods
//--------------------------------------------------------------------------------
Console.WriteLine("\n............ Selected Methods .......................\n");

//--------------------------------------------------------------------------------
Console.WriteLine("GetDayOfYear: day = {0}", jc.GetDayOfYear(thisDate));
//--------------------------------------------------------------------------------
Console.WriteLine("GetDaysInMonth: days = {0}",
jc.GetDaysInMonth( thisDate.Year, thisDate.Month,
PersianCalendar.PersianEra));
//--------------------------------------------------------------------------------
Console.WriteLine("GetDaysInYear: days = {0}",
jc.GetDaysInYear(thisDate.Year, PersianCalendar.PersianEra));
//--------------------------------------------------------------------------------
Console.WriteLine("GetLeapMonth: leap month (if any) = {0}",
jc.GetLeapMonth(thisDate.Year, PersianCalendar.PersianEra));
//-------------------------------------------------------------
Console.WriteLine("GetMonthsInYear: months in a year = {0}",
jc.GetMonthsInYear(thisDate.Year, PersianCalendar.PersianEra));
//--------------------------------------------------------------------------------
Console.WriteLine("IsLeapDay: This is a leap day = {0}",
jc.IsLeapDay(thisDate.Year, thisDate.Month, thisDate.Day,
PersianCalendar.PersianEra));
//--------------------------------------------------------------------------------
Console.WriteLine("IsLeapMonth: This is a leap month = {0}",
jc.IsLeapMonth(thisDate.Year, thisDate.Month,
PersianCalendar.PersianEra));
//--------------------------------------------------------------------------------
Console.WriteLine("IsLeapYear: 1370 is a leap year = {0}",
jc.IsLeapYear(1370, PersianCalendar.PersianEra));
//--------------------------------------------------------------------------------

// Get the 4-digit year for a year whose last two digits are 99. The 4-digit year
// depends on the current value of the TwoDigitYearMax property.

Console.WriteLine("ToFourDigitYear:");
Console.WriteLine(" If TwoDigitYearMax = {0}, ToFourDigitYear(99) = {1}",
jc.TwoDigitYearMax, jc.ToFourDigitYear(99));
jc.TwoDigitYearMax = thisDate.Year;
Console.WriteLine(" If TwoDigitYearMax = {0}, ToFourDigitYear(99) = {1}",
jc.TwoDigitYearMax, jc.ToFourDigitYear(99));
}
}
/*
The example produces the following results:

................. Today ...........................

Today is Wednesday, 6/14/2006 11:31:13 AM

............... Fields .............................

PersianEra = 1

............... Properties .........................

Eras: era = 1
MaxSupportedDateTime = 12/31/9999 11:59:59 PM
MinSupportedDateTime = 3/21/0622 12:00:00 AM
TwoDigitYearMax = 1410

................ Methods ...........................

AddMonths: thisDate + 6 months = 12/15/2006 11:31:13 AM
thisDate - 6 months = 12/15/2005 11:31:13 AM
AddYears: thisDate + 5 years = 6/14/2011 11:31:13 AM
thisDate - 3 years = 6/14/2003 11:31:13 AM
GetDayOfMonth: month = 24
GetDayOfWeek: day = Wednesday
GetDayOfYear: day = 86
GetDaysInMonth: days = 31
GetDaysInYear: days = 366
GetEra: era = 1
GetLeapMonth: leap month (if any) = 0
GetMonth: month = 3
GetMonthsInYear: months in a year = 12
GetYear: year = 1385
IsLeapDay: This is a leap day = False
IsLeapMonth: This is a leap month = False
IsLeapYear: 1370 is a leap year = True
ToDateTime:
Gregorian calendar: 6/10/2006 8:30:15 PM
Persian calendar: 3/20/1385 8:30:15 PM
ToFourDigitYear:
If TwoDigitYearMax = 1410, ToFourDigitYear(99) = 1399
If TwoDigitYearMax = 2006, ToFourDigitYear(99) = 1999

*

bitcob589
یک شنبه 21 اسفند 1390, 19:43 عصر
با سلام
کلاس به صورت نوشته ولی خطا می دهد علت خطا چیست

public class barnamhnevis
{
prviate DateTime datatimestart
public barnamhnevis(DateTime Datatimestart)
{
datatimestart=Datatimestart
}
public DateTime Datatimestart
{
get
{
PersianCalenderjc=new PersianCalender();
DateTime dt=new DateTime();
return consol.write("{0},{1},{2},{3},{4},{5},{6},dt,js);
}
}