Today is 1397/02/29 (yyyy/mm/dd) in Persian calendar.
and my website reports his error:
'Year, Month, and Day parameters describe an un-representable DateTime.'
I used datetime2 in SQL database and saved Persian date in it by converting this date:
PersianCalendar pc = new PersianCalendar();
DateTime dateTime = DateTime.Now;
return new DateTime(pc.GetYear(dateTime), pc.GetMonth(dateTime), pc.GetDayOfMonth(dateTime), pc.GetHour(dateTime), pc.GetMinute(dateTime), pc.GetSecond(dateTime), 0);
What should i do?
Quite ignorant here... but in general you should treat "internally" (in the program and in the db) all the dates using the normal Proleptic Gregorian calendar (per ISO 8601), and reformat them to Persian only when you read input from the user/write output to the user. There is a PersianCalendar class in .NET that helps converting persian-to-american and back.
For example
var pc = new PersianCalendar();
DateTime dt = pc.ToDateTime(1397, 02, 29, 0, 0, 0, 0);
works correctly, but if you take a look at the dt variable you'll see that it shows as 2018-05-19.
If doing a ToString() on a DateTime variable you see a persian date, then you probably have an error in your code. If doing a SELECT on a table in the db you see a persian date, then you probably have an error in your db.
The interesting paradox is that internally both .NET and SQL save dates as a single number, representing how many units of time (where in .NET a unit of time is 100 nanoseconds, in SQL it is 1 day) have passed from a "zero point" to the date, but then all their "standard" methods for handling dates (both in .NET and SQL) use the gregorian calendar (because it is the most used in the world).
Use this solution
static void Main(string[] args)
{
Console.WriteLine(GetIsLeapDateTime(1358));
Console.WriteLine(GetIsLeapDateTime(1359));
Console.WriteLine(GetIsLeapDateTime(1360));
Console.WriteLine(GetIsLeapDateTime(1361));
Console.WriteLine(GetIsLeapDateTime(1362));
Console.WriteLine(GetIsLeapDateTime(1363));
Console.WriteLine(GetIsLeapDateTime(1364));
Console.WriteLine(GetIsLeapDateTime(1365));
Console.WriteLine(GetIsLeapDateTime(1366));
Console.ReadLine();
}
private static bool GetIsLeapDateTime(int year)
{
PersianCalendar pc = new PersianCalendar();
return pc.IsLeapYear(year);
}
in Jalali calendar
It seems that every 33 years, leap years happen once every 5 years and then back to once every 4 years.
1370 is a leap year, but 1374 is not a leap year, and instead, 1375 is a leap year. Also, 1403 is not a leap year and 1407 is not a leap year, and instead, 1408 leap years.
Related
Using c#, I want to compare the current week-to-date to the same period last week-to-date. For example, if today is Wednesday, and if the first day of the week is Sunday, then I want to compare totals for Sunday – Tuesday of this week against Sunday – Tuesday of last week. I’m not counting Wednesday because I don’t have a full day of data until midnight the same day.
The same applies to comparing this mtd to the same number of days last month, and last year. For example, if the current date is June 19th, I want to compare the data from May 1-18th of last month as well as January 1 – June 18th of last year against January 1 – June 18th of this year.
The variables I’m trying to use look like this:
//Current dates
DateTime currentDte = DateTime.Now;
DateTime beginWeek = DateTime.Now.StartOfWeek(DayOfWeek.Sunday);
DateTime beginMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
DateTime beginYear = new DateTime(DateTime.Now.Year, 1, 1);
//Historical dates
DateTime lastWeekToDate = DateTime.Now.StartOfWeek(DayOfWeek.Sunday - (7 - (int)DateTime.Now.DayOfWeek));
DateTime lastMonthToDate =
DateTime lastYearToDate =
As you can see I figured out the current dates and can loop through them to get the wtd, mtd, and ytd data I need. And I managed to figure out how to get the last week-to-date I need.
But I don’t know how to get the lastMonthToDate and lastYearToDate dates I need. I’ve tried everything I can think of. I’ve read date documentation until my eyes hurt, and still I come up with goose eggs. Can anyone offer any suggestions?
If I understand your question correctly, you just need to use AddMonths and AddYears.
DateTime lastMonthToDate = beginMonth.AddMonths(-1);
DateTime lastYearToDate = beginYear.AddYears(-1);
EDIT
Based your comment - it looks like you want the to subtract months/years from the current date in which case it would be
DateTime lastMonthToDate = currentDte.AddMonths(-1);
DateTime lastYearToDate = currentDte.AddYears(-1);
If it's the time of day that's throwing you off, just use the date part DateTime.Now.Date.AddMonths(-1)
Based on the feedback I received from Zeph, I was able to get the beginning of last year using this code:
DateTime startDate = beginYear.AddYears(-1);
Now I'm able to tally all of the ytd data I need based on the interval dates. Thank you very much!
I cannot understand or find any information that could explain why there are two different time component output (12p.m and 11 a.m) for the following. Can somebody please explain.
DateTime d1 = new DateTime(2015, 05, 15).ToUniversalTime();
DateTime d2 = new DateTime(2015, 02, 02).ToUniversalTime();
Console.WriteLine(d1.ToString()); //OUTPUTS - 1/05/2015 12:00:00 p.m.
Console.WriteLine(d2.ToString()); //OUTPUTS - 1/02/2015 11:00:00 a.m.
The ToUniveralTime method converts from the local time zone where the code is running, to UTC.
Since time zones can change their offsets from UTC at different times of the year, the value can easily be different between two different dates - especially since one date is in the winter, and the other is in the summer, due to daylight saving time.
See also, the DST tag wiki, and "time zone != offset" in the timezone tag wiki.
I am trying to convert to Julian Time Stamp to Date Time. I have the following microseconds time stamp 212302469304212709. As i understand i need to add these milliseconds to the beginning of Julian Calendar (January 1, 4713 B.C., 12:00 (noon)). So i have the following method:
private DateTime GetDateTime(string julianTimeStamp)
{
var julianMilliseconds = Convert.ToDouble(julianTimeStamp)/1000;
var beginningOfTimes = new DateTime(1, 1, 1, 0, 0, 0, 0);
var dateTime = beginningOfTimes.AddMilliseconds(julianMilliseconds).AddYears(-4713).AddMonths(-1).AddDays(-1).AddHours(-12);
return dateTime;
}
Assume i pass 212302469304212709 string as the parameter. The expected result should be 2015/07(July)/01 00:08:24.212. Based on my method, i have almost the same result, but day is not 1, it is 6. Same problem for different time stamps i tested.
Could any one tell me what i am doing wrong? Thanks in advance.
Edited:
This is the exact date time i expect to receive: 2015(year) 7(month) 1(day) 0(hour) 8(minute) 24(second) 212(millisecond) 709(microsecond)
The given timestamp 212,302,469,304,212,709 μs when converted to days (just divide by 86,400,000,000) gives 2457204.505836 days (to six decimal places, which is the best I can do without a lot of extra trouble). Using the Multi Year Computer Interactive Almanac (MICA) written by the United States Naval Observatory, and putting in the free form date 2015(year) 7(month) 1(day) 0(hour) 8(minute) 24(second) 212(millisecond) 709(microsecond), the program calculates exactly the same day count (to six decimal places), proving the time stamp is an accurate Julian date.
One problem with the OP's calculation is trying to use the DateTime class before the earliest supported date, as pointed out by another poster. Also, the OP didn't say if 1 July 2015 was in the Julian or Gregorian calendar, but the MICA calculation proves it is in the Gregorian calendar. Since the OP is working in the Gregorian calendar, the epoch of Julian dates should be stated in the Gregorian proleptic calendar: Noon Universal Time, November 24, 4714 BC. The oft-quoted date January 1, 4713 BC is a proleptic Julian calendar date.
"Proleptic" means a date has been found by beginning at a modern date, who's calendar date is known with absolute certainty, and applying the rules of the chosen calendar backward until the desired date is reached, even though the desired date is before the chosen calendar was invented.
DateTime uses Gregorian calendar, so when you substract years, months and so on you are doing it with that calendar, not the Julian.
Unfortunately DateTime does not support dates before year 1. You can check the library in this post, maybe it helps you.
I have two dates
DateTime date1Z = DateTime.Parse("2014-05-22 23:39:29Z");
DateTime date1ZKind = DateTime.SpecifyKind(DateTime.Parse("2014-05-22 23:39:29Z"), DateTimeKind.Utc);
DateTime date2 = DateTime.Parse("2014-05-22 23:39:29");
DateTime date2Kind = DateTime.SpecifyKind(DateTime.Parse("2014-05-22 23:39:29"), DateTimeKind.Utc);
Console.WriteLine(date1Z);
Console.WriteLine(date1ZKind);
Console.WriteLine(date2);
Console.WriteLine(date2Kind);
Prints
23/05/2014 11:39:29 a.m.
23/05/2014 11:39:29 a.m.
22/05/2014 11:39:29 p.m.
22/05/2014 11:39:29 p.m.
Can someone explain whats going on here?
Using the suffix "Z" is date shorthand for saying that the Date-Time is "Zulu" time which is another word for UTC time. The first two dates are being parsed as UTC, while the last two are being parsed as whatever time is on the computer in question.
So to answer you question of what is going on: the latter two dates are being offset by your local time, which is apparently +12:00 (plus twelve hours), while the first two are not (as they are marked as "Zulu" or UTC time).
You live in New Zealand, which is +12 over UTC. That matches the date difference you are experiencing. As mentioned, the Z stands for UTC.
I'm trying to find the time between two dates, one being the current day (Today) and the other, a user defined deadline.
I'm working with C# Windows Forms and I've used a "date time picker" so that the user can choose a deadline date and I've created a string called Today and used
string Today = System.DateTime.Today.ToString("dd-mm-yyyy");
as the current date. But I don't know how to find the length of time between these two points (since they're strings), my program is a simple "to do list" where task duration's are in days and weeks (the "yyyy" is just for aesthetic purposes, it can be removed if necessary).
I've had a look over the internet and all I can seem to find is how to do this with "DateTime"s, instead of strings (or am I missing something?).
Any help would be greatly apreciated.
Use the 'Value' property of your DateTimePicker to get the DateTime value and use DateTime.Now to get the DateTime value for the current time (in the local timezone).
If you are only subtracting dates (with no time component), access the Date property of both DateTime objects before subtracting.
DateTime userDate = dateTimePicker.Value.Date;
DateTime currentDate = DateTime.Now.Date;
TimeSpan difference = userDate.Subtract(currentDate); //assuming deadline is in the future
Don't use two strings - use the actual DateTime instances. Strings don't and can't "understand" dates - that's why the DateTime object exists.
When you subtract two dates from each other, you get a TimeSpan instance. This gives you the amount of time difference.
TimeSpan difference = date1 - date2;
I guess the following you are looking for:
Date1.Subtract(Date2).TotalTime
Following link will help you understand more
http://www.c-sharpcorner.com/UploadFile/DipalChoksi/DateDiff_CS_DC09132006172429PM/DateDiff_CS_DC.aspx
AS Oded mentions the string is not the best approach. DateTimes cann be substracted from one another and give you a TimeSpan that represents, well, that, a time span.
Here is an MSDN example that should clarify things.
DateTime departure = new DateTime(2010, 6, 12, 18, 32, 0);
DateTime arrival = new DateTime(2010, 6, 13, 22, 47, 0);
TimeSpan travelTime = arrival - departure;
Console.WriteLine("{0} - {1} = {2}", arrival, departure, travelTime);
// The example displays the following output:
// 6/13/2010 10:47:00 PM - 6/12/2010 6:32:00 PM = 1.04:15:00