Now i am working with Hijri dates and trying to convert them to Gregorian dates using the following code :
string HijriDate;
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","MM/dd/yyyy"};
CultureInfo enCul = new CultureInfo("en-US");
CultureInfo arCul = new CultureInfo("ar-SA");
arCul.DateTimeFormat.Calendar = new System.Globalization.HijriCalendar();
DateTime tempDate = DateTime.ParseExact(HijriDate, allFormats, arCul.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces);
return tempDate.ToString("MM/dd/yyyy");
this code is working fine with all dates except the date that has 30th day in month like the following :
30/10/1433, 30/12/1432 or 30/05/1433 etc. so how to handle and convert that date with its corresponding Gregorian
here is the code it is working well
now on this code I'm returning the date from the function as string not as datetime, but you can simply using return datetime type instead on string
public string ConvertDateCalendar(DateTime DateConv, string Calendar, string DateLangCulture)
{
System.Globalization.DateTimeFormatInfo DTFormat;
DateLangCulture = DateLangCulture.ToLower();
/// We can't have the hijri date writen in English. We will get a runtime error - LAITH - 11/13/2005 1:01:45 PM -
if (Calendar == "Hijri" && DateLangCulture.StartsWith("en-"))
{
DateLangCulture = "ar-sa";
}
/// Set the date time format to the given culture - LAITH - 11/13/2005 1:04:22 PM -
DTFormat = new System.Globalization.CultureInfo(DateLangCulture, false).DateTimeFormat;
/// Set the calendar property of the date time format to the given calendar - LAITH - 11/13/2005 1:04:52 PM -
switch (Calendar)
{
case "Hijri":
DTFormat.Calendar = new System.Globalization.HijriCalendar();
break;
case "Gregorian":
DTFormat.Calendar = new System.Globalization.GregorianCalendar();
break;
default:
return "";
}
/// We format the date structure to whatever we want - LAITH - 11/13/2005 1:05:39 PM -
DTFormat.ShortDatePattern = "dd/MM/yyyy";
return (DateConv.Date.ToString("f", DTFormat));
}
To call this Method here are an example
ltrCalValue.Text = ConvertDateCalendar(CalHijri.SelectedDate, "Gregorian", "en-US");
To call the Hijri
ltrCalValue.Text = ConvertDateCalendar(CalHijri.SelectedDate, "Hijri", "en-US");
Max Value of a days in a month can be calculated by DateTime.DaysInMonth(year, month)
and use it this way
int result = DateTime.DaysInMonth(2012, 2); // returns 29 being a leap year
but
int result = DateTime.DaysInMonth(2011, 2) // returns 28 being a non-leap year
10th and 12nd months in Hirji don't have 30th day, so that date is invalid.
Related
I have a JS function that generates today date:
function GetDate(date) {
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
today = dd + '/' + mm + '/' + yyyy;
alert(today);
return today; // 13/03/2021
}
This function returns 13/03/2021
I pass it on to Server Side Code and do this :
DateTime dateToday = DateTime.ParseExact(cdate, "dd/MM/yyyy", CultureInfo.GetCultureInfo("en-AU"));
emailCopy = emailCopy.Replace("{date}", dateToday.ToString("dd MMMM yyyy"));
However here it puts the date as 12 March 2021
Why is it doing that? The date going in is clearly 13/03/2021. Also in next line I pass this date to be added to SQL Server Table:
dateToday.ToString("yyyy-MM-dd")
And the date added to the database is also correct : 2021-03-13.
When you create a new DateTime object, but only set the date part of it, this sets the time to 00:00:00 (midnight). This is in GMT. So when you format the date it takes the date you set at midnight, and converts it to your time zone, which is actually the day before.
You can fix this by doing this "kludge":
var now = DateTime.Now;
var adjusted = new DateTime(
dateToday.Year, dateToday.Month, dateToday.Day, now.Hour, now.Minute, now.Second);
var final = adjusted.ToString("dd MMMM yyyy");
There may be a better way to do this, though.
ETA
You should consider using JavaScript's Date.toISOString() instead of just sending the date. Then in C#, use Convert.ToDateTime() to parse it. That uses UTC and you are guaranteed to get the exact time that the client machine generated the date.
My Problem: I want to convert an german date "24.05.05" to the UTC-Format "2005-05-24". In German date format "24.05.05" the last two digits is the year 2005.
Here is my code which not works:
var lGermanDate = "24.05.05";
DateTime lOutDateTime;
CultureInfo lCultureInfo = new CultureInfo("de-de");
// expecting result to fail
if (DateTime.TryParseExact(lGermanDate, lCultureInfo.DateTimeFormat.ShortDatePattern, lCultureInfo, DateTimeStyles.None, out lOutDateTime))
{
var lTargetDate = lOutDateTime.ToString("yyyy-m-d");
}
else
{
[...]
}
Note: in PHP this works with the following code:
\DateTime::createFromFormat('d.m.y', $lGermanDate )->format('Y-m-d');
Have you try the following?
//To Convert lGermanDate into DateTime
string DATEPATTERN = "dd.MM.yy";
DateTime.TryParseExact(lGermanDate, DATEPATTERN, null, DateTimeStyles.None, out DateTime outGermanDate);
//From outGermanDate to UTC Format
string dateUTC = outGermanDate.ToString("yyyy-MM-dd")
Your parsing is fine, maybe a little too specific
m is minutes is DateTime.ToString() patterns, M is month
var germanDateStr = "24.05.05";
if (DateTime.TryParse(germanDateStr, out DateTime outDateTime))
{
var targetDate = outDateTime.ToString("yyyy-M-d");
targetDate.Dump();
}
else
{
}
I am trying to convert a string (which represents date in invariantCulture) to dateTime in given culture. The problem is that when the date is converted to German culture, the day becomes month and month becomes day.
What is wrong with below code or am i missing something ?
var day = 11; var month = 12; var year = 2014;
var someDate = new DateTime(year, month, day);
var theDay = someDate.Day;//11 ok as expected
var theMonth = someDate.Month; //12 ok as expected
var dateString = someDate.ToString(CultureInfo.InvariantCulture);
var date1 = DateTime.Parse(dateString, CultureInfo.GetCultureInfo("de-De"));
var day1 = date1.Day;//12 this should be 11 ?
var month1 = date1.Month; //11 this should be 12 ?
The second argument to DateTime.Parse is used to tell the parser what format the string is in, not what format you want to convert it to. You are generating an invariant string and then parsing it as a German string which is why your day and month are getting swapped.
If your goal is to get a German string representation of the date, just use var dateString = someDate.ToString(CultureInfo.GetCultureInfo("de-DE")).
I guess de-De culture doesn't have a standard date and time format as MM/dd/yyyy HH:mm:ss.
Since you using DateTime.ToString() method with InvariantCulture, result string will be "G" standard format which is MM/dd/yyyy HH:mm:ss for InvariantCulture.
Because of that, dateString will be 12/11/2014 00:00:00 and de-DE culture doesn't have a standard date and time format MM/dd/yyyy HH:mm:ss but has dd/MM/yyyy HH:mm:ss which is dd.MM.yyyy HH:mm:ss for de-DE culture.
That's why DateTime.Parse method matches pattern which is dd/MM/yyyy HH:mm:ss (since it's DateSeparator is . it should be dd.MM.yyyy HH:mm:ss format).
That's why it parses your 12 as a Day and 11 as a Month.
If you already a DateTime (which you have) just use .ToString() method with your de-DE culture like;
var culture = new CultureInfo("de-De");
var dateString = someDate.ToString(culture);
Remember, a DateTime doesn't have any implicit format or culture. It just have date and time values. String representations of them can have formats.
By the way, you can find all standard date and time patterns your de-DE culture like;
var culture = new CultureInfo("de-De");
foreach (var format in culture.DateTimeFormat.GetAllDateTimePatterns())
{
Console.WriteLine(format);
}
Change the following line and test it again:
var dateString = someDate.ToString(CultureInfo.InvariantCulture);
to:
var dateString = someDate.ToString("O");
or:
var dateString = someDate.ToString("S");
ok, here is what i think what you want to accomplish, not sure if i got you right: you want to read an invariant cultured date string and convert it to a german cultured date string.
but in your example you are trying to parse an invariant cultured date AS a german cultured date. of course that leads to a misinterpretation. try this:
string invariantCultureDateString = "12/11/2014 00:00:00";
var dateTime = DateTime.Parse(invariantCultureDateString, CultureInfo.InvariantCulture);
string germanCultureDateString = dateTime.ToString(CultureInfo.GetCultureInfo("de-De"));
BR
Hi I have julian date string YYJJJ format. eg 05365(31st dec 2005). I want to covert to MMDDYY format(123105).
Is there any defined function for that in?
I faced same problem as I was try to convert dates from BACS 18 standard to a String. I couldn't find ready solution to this problem so I wrote this function:
private String bacsDateConvert(String bacsFormatDate)
{
int dateYear = Convert.ToInt16(bacsFormatDate.Substring(1, 2));
int dateDays = Convert.ToInt16(bacsFormatDate.Substring(3, 3));
DateTime outputDate = new DateTime();
outputDate = Convert.ToDateTime("31-12-1999");
outputDate = outputDate.AddYears(dateYear);
outputDate = outputDate.AddDays(dateDays);
String outputString = outputDate.ToString("yyyyMMdd");
return outputString;
}
//You may call it like this:
textBox4.Text = Convert.ToString(bacsDateConvert(bacsTxnValueDate));
You also may modify it slightly and easily make it return DateTime data type if you want to. I just needed to return a string in the above format.
First of all, there is no YY, JJJ and DD formats as a custom date and time format. One solution might be to split your string Year and DayOfYear part and create a DateTime with JulianCalendar class.
string s = "05365";
int year = Convert.ToInt32(s.Substring(0, 2));
// Get year part from your string
int dayofyear = Convert.ToInt32(s.Substring(2));
// Get day of years part from your string
DateTime dt = new DateTime(1999 + year, 12, 18, new JulianCalendar());
// Initialize a new DateTime one day before year value.
// Added 1999 to year part because it makes 5 AD as a year if we don't.
// In our case, it is 2004/12/31
dt = dt.AddDays(dayofyear);
// Since we have a last day of one year before, we can add dayofyear to get exact date
I initialized this new DateTime(.. part with 18th December because
From Julian Calendar
Consequently, the Julian calendar is currently 13 days behind the
Gregorian calendar; for instance, 1 January in the Julian calendar is
14 January in the Gregorian.
And you can format your dt like;
dt.ToString("MMddyy", CultureInfo.InvariantCulture) //123105
I honestly didn't like this way but this is the only one I can imagine as a solution.
This question already has answers here:
How to remove time portion of date in C# in DateTime object only?
(43 answers)
Closed 9 years ago.
The line of code DateTime d = DateTime.Today; results in 10/12/2011 12:00:00 AM. How can I get only the date part.I need to ignore the time part when I compare two dates.
DateTime is a DataType which is used to store both Date and Time. But it provides Properties to get the Date Part.
You can get the Date part from Date Property.
http://msdn.microsoft.com/en-us/library/system.datetime.date.aspx
DateTime date1 = new DateTime(2008, 6, 1, 7, 47, 0);
Console.WriteLine(date1.ToString());
// Get date-only portion of date, without its time.
DateTime dateOnly = date1.Date;
// Display date using short date string.
Console.WriteLine(dateOnly.ToString("d"));
// Display date using 24-hour clock.
Console.WriteLine(dateOnly.ToString("g"));
Console.WriteLine(dateOnly.ToString("MM/dd/yyyy HH:mm"));
// The example displays the following output to the console:
// 6/1/2008 7:47:00 AM
// 6/1/2008
// 6/1/2008 12:00 AM
// 06/01/2008 00:00
There is no way to "discard" the time component.
DateTime.Today is the same as:
DateTime d = DateTime.Now.Date;
If you only want to display only the date portion, simply do that - use ToString with the format string you need.
For example, using the standard format string "D" (long date format specifier):
d.ToString("D");
When comparing only the date of the datatimes, use the Date property. So this should work fine for you
datetime1.Date == datetime2.Date
DateTime d = DateTime.Today.Date;
Console.WriteLine(d.ToShortDateString()); // outputs just date
if you want to compare dates, ignoring the time part, make an use of DateTime.Year and DateTime.DayOfYear properties.
code snippet
DateTime d1 = DateTime.Today;
DateTime d2 = DateTime.Today.AddDays(3);
if (d1.Year < d2.Year)
Console.WriteLine("d1 < d2");
else
if (d1.DayOfYear < d2.DayOfYear)
Console.WriteLine("d1 < d2");
you can use a formatstring
DateTime time = DateTime.Now;
String format = "MMM ddd d HH:mm yyyy";
Console.WriteLine(time.ToString(format));