I have a datetime column in database.
DateTime end_date = DateTime.ParseExact("2013-Jan-31", "yyyy-MM-dd", CultureInfo.InvariantCulture);
Why isn't this working?
This is not working because MM would mean January to be 01. If this is the format of the date you're trying to parse, try the format "yyyy-MMM-dd".
Hope this helps
Try like this;
DateTime a = DateTime.ParseExact("2013-Jan-31", "yyyy-MMM-dd", System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine (a);
Output:
31.01.2013
Look at from MSDN Custom Date and Time Format Strings
To use such a name of the month you need to take "MMM" so it will be
myObject.end_date = DateTime.ParseExact("2013-Jan-31", "yyyy-MMM-dd", System.Globalization.CultureInfo.InvariantCulture);
MM represents a two-digit numerical month (such as "01").
MMM represents the abbreviated month (such as "Jan").
Which means that you need
myObject.end_date = DateTime.ParseExact("2013-Jan-31", "yyyy-MMM-dd", System.Globalization.CultureInfo.InvariantCulture);
See http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx for a list of string format specifiers.
Related
I am Trying to Convert Hijri Date into Gregorian Date I was following this article and My Code is as follows :
var cultureInfo = CultureInfo.CreateSpecificCulture("ar-sa");
string date = "19/12/36 12:00:00 ص";
Getting
string was not recognized as a valid datetime
error in below line
DateTime tempDate = DateTime.ParseExact(date, "dd/MM/yyyy", cultureInfo.DateTimeFormat, DateTimeStyles.AllowInnerWhite);
lblDate.Text = tempDate.ToString("dd/MM/yyyy");
I am getting string was not recognized as a valid datetime. Please can somebody tell me whats wrong with this code?
I think I'm on the right way but.. Let's try something at least.
First of all, DateTime values are always in the Gregorian calendar, basically. There's no such thing as "A DateTime in a UmAlQuraCalendar calendar" - which is used by ar-sa culture - you have to use the UmAlQuraCalendar to interpret a DateTime in a particular way.
Second, when you use DateTime.ParseExact for parsing your string, your string and format does match exactly based on culture you use. Since ص
character seems AMDesignator of ar-sa culture, you should provide tt specifier with your time part as well.
string s = "19/12/36 12:00:00 ص";
DateTime dt;
if(DateTime.TryParseExact(s, "dd/MM/yy hh:mm:ss tt", CultureInfo.GetCultureInfo("ar-sa"),
DateTimeStyles.None, out dt))
{
Console.WriteLine(dt);
}
Note: Since TwoDigitYearMax is 1451 of UmAlQuraCalendar calendar, your 36 will be parsed as 1436 with yy format specifier.
This perfectly parse your question but WAIT! What will be the result? Here it is.
02/10/2015 00:00:00
Why? As I said in the top, you have to use the UmAlQuraCalendar to interpret this DateTime instance.
UmAlQuraCalendar ul = new UmAlQuraCalendar();
Console.WriteLine(ul.GetYear(dt)); // 1436
Console.WriteLine(ul.GetMonth(dt)); // 12
Console.WriteLine(ul.GetDayOfMonth(dt)); // 19
I want to get the last seen of user and save it to my sql database in mvc5 . I got the last seen in controller with code like this:
users.userlast=DateTime.Now;
and saved to my database in this format "2015-08-06 12:12:13.443". I want to get datetime only format day,month,year, hour and minute.
I can't use something like this,
var dateTime = DateTime.ParseExact("12/02/21 10:56:09", "yy/MM/dd HH:mm", CultureInfo.InvariantCulture);
var text = dateTime.ToString("MMM. dd, yyyy HH:mm");
It did not work because my last seen column is a datetime type not string. What should i do?
Thanks.
Edit:
Like whatsup App., i want to see only hour and minute, not seconds as last seen.
You say that you are storing as a datetime type, in which case the you shouldn't need to convert a string to a DateTime. In fact you shouldn't need to do any parsing.
When you query the database you should get a DateTime, on which you can call the ToString() you want.
to get datetime in format day, month, year, hour and minute only (without seconds, milliseconds), create a new DateTime value before save:
var dt = DateTime.Now;
users.userlast = dt.Date.AddHours(dt.Hour).AddMinutes(dt.Minute);
You don't have to worry about the format you save in the database. When you want to represent it in your specific format you can ToString it accordingly.
I want to get datetime only format day,month,year, hour and minute.
string text = dateTime.ToString("yyyy-MM-dd HH:mm",
CultureInfo.InvariantCulture);
CultureInfo.InvariantCulture is to use your specified culture regardless of the user's current culture.
You can use InvariantCulture because your user must be in a culture that uses a dot instead of a colon:
DateTime.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);
Just do like that
var formattedDateTime = yourLastSeenDateTime.ToString("MMM. dd, yyyy HH:mm", CultureInfo.InvariantCulture);
EDIT: Try this as you mentioned in comments
DateTime dbDate = yourLastSeenDateTime;
DateTime newDateTime = new DateTime(dbDate.Year, dbDate.Month, dbDate.Day, dbDate.Hour, dbDate.Minute, 0);
I have DateTime variable in this format "dd/mm/yyyy hh:mm:ss".
I would like to convert this to "mm/dd/yyyy hh:mm:ss". (Month before day)
I tried to use DateTime.parse like this:
DateTime dt = new DateTime();
dt = dateTimePicker1.Value;
dt = DateTime.Parse(dt.ToString(), "mm/dd/yyyy", null);
but it wont work.
I also try it with DateTime.ParseExact() but still nothing happened.
Any suggestion?
A DateTime doesn't have any implicit format. It just has date and time values.
Formatting subject only applies when you try to get it's textual representation. If you wanna get string represetation of it, you can use .ToString() method like;
string s = dateTimePicker1.Value.ToString("MM/dd/yyyy hh:mm:ss",
CultureInfo.InvariantCulture);
And remember, mm specifier is for minutes, MM specifier is for months. Also hh specifier is for 12-hour clock and HH specifier is for 24-hour clock representations. You might wanna use HH instead.
I used InvariantCulture as a second parameter because / and : have special meaning as replace me current culture or supplied culture date or time separator.
string date = dateTimePicker1.Value.ToString("MM/dd/yyyy");
You can also use this below code for advance:
GregorianCalendar cal = new GregorianCalendar(); // or any type of calendar you want ...
string formatted = string.Format("{1:00}/{2:00}/{0} {3:00}:{4:00}:{5:00} ",
cal.GetYear(dt), cal.GetMonth(dt), cal.GetDayOfMonth(dt) , cal.GetHour(dt), cal.GetMinute(dt), cal.GetSecond(dt));
DateTime time = DateTime.Now;
string format = "MMM ddd d HH:mm yyyy";
Console.WriteLine(time.ToString(format));
i have a text-box in a detailview and the value of the text-box is a Date but it only shows the Month and Year and it is like this:November 2013 so i want to take this value and convert like this: 20131101. So as you can see, i would like the format to be YYYYMMDD but the day should always be 01 which is the first of the month. So how can i go from this November 2013 to this 20131101? here is my code and i know i have to convert from string to date first:
string myDate = ((TextBox)DetailView1.FindControl("InputDate")).Text.ToString();
Convert it:
TextBox txtInputDate = (TextBox)DetailView1.FindControl("InputDate");
DateTime dt = DateTime.ParseExact(txtInputDate.Text, "MMMM yyyy", CultureInfo.InvariantCulture);
then convert it to string again:
txtInputDate.Text = dt.ToString("yyyyMMdd", CultureInfo.InvariantCulture);
C# is pretty good at parsing stringy dates, you could lean on the build it parsing:
string myDateString = ((TextBox)DetailView1.FindControl("InputDate")).Text.ToString();
DateTime myDate;
if (DateTime.TryParse(myDateString, out myDate)) {
// myDate now contains a proper .NET date.
}
Now you have a proper DateTime, you can output it in any format you like.
DateTime test = DateTime.Parse("November 2013");
Console.WriteLine(test.ToString("yyyyMMdd"));
Use the DateTime.ParseExact() method, like this:
var theParsedDate = DateTime.ParseExact(myDate, "MMMM yyyy",
CultureInfo.InvariantCulture);
Now you can use the parsed date however you wish, convert it to string, send it to database, etc.
I am trying to convert a string value into DateTime. It gives me an error as, specified string is not in correct format.
Here is the code,
DateTime myDate = DateTime.ParseExact("07-09-2013 01:14:14:1414", "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
string strDate = "07/09/2013 01:04:02:4";
Convert.ToDateTime(strDate);
DateTime dt = DateTime.Parse(strDate);
Please help in converting the same.
Thanks
Your format seems to be incorrect. Should be:
"dd-MM-yyyy HH:mm:ss:ffff"
Update. If number of digits representing fractions of a second varies, than the best bet is
"dd-MM-yyyy HH:mm:ss:FFFFFFF"
Refer to MSDN for other options for custom time and date formats.
Try in your page_load event:
Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo("tr-TR")
Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("tr-TR")
First, you mixed you years place
07-09-2013 is dd-MM-yyyy format
second, you need a :ffff after seconds
So the final line should be
DateTime myDate = DateTime.ParseExact("2013-07-09 01:14:14:1414", "yyyy-MM-dd HH:mm:ss:ffff", System.Globalization.CultureInfo.InvariantCulture);
Your format isn't correct:
"07-09-2013 01:14:14:1414"
"yyyy-MM-dd HH:mm:ss"
Atleast your date is the other way around, and the milliseconds is not specified.
Correct you format according to this: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
For the downvoter:
The correct format is specified by Andrei: "dd-MM-yyyy HH:mm:ss:ffff"
First of all, you should change your date format to dd-MM-yyyy because it fits with your date format.
Second of all, for miliseconds part, you can use . instead of :
DateTime myDate = DateTime.ParseExact("07-09-2013 01:14:14.1414", "dd-MM-yyyy HH:mm:ss.ffff", System.Globalization.CultureInfo.InvariantCulture);
string strDate = "07/09/2013 01:04:02.4";
Convert.ToDateTime(strDate);
DateTime dt = DateTime.Parse(strDate);
Here a DEMO.
For more information, check Custom Date and Time Format Strings