I am writing this code the text Change event but it is showing the error that is string is not valid.
DateTime ts=Convert.ToDateTime(Joiningdate.Text);
DateTime dt1=ts.AddMonths(6);
txtcd.Text = dt1.ToShortDateString();
The DataTime format does not match the format you need to convert string to DateTime object. You can use DateTime.ParseExact() to give you format you date you have.
Assuming you have format dd/MM/YY for the textbox
DateTime ts= DateTime.ParseExact(Joiningdate.Text, "dd/MM/yy", CultureInfo.InvariantCulture);
DateTime dt1=ts.AddMonths(6);
txtcd.Text = dt1.ToShortDateString();
The problem is with the format of Joiningdate.Text
You will have to tell that in what format your date time is.
If your date time is in format use
DateTime ts= DateTime.ParseExact(Joiningdate.Text, "ddMMyyyy",
CultureInfo.InvariantCulture);
DateTime dt1=ts.AddMonths(6);
nd then you can convert back to any format you want
ts.ToString("yyyyMMdd");
You can go through this link
Convert DateTime to string format("yyyyMMdd")
Related
I want to change datetime format so that first of all I got datetime and after that I have convert it to string with different format. Now I want to again convert this into datetime format but this gives an error, invalid date time. My code is:
DateTime startDT2 = DateTime.newInstance(selectedDate.addDays(1), initialEndTime);
system.debug('select dt2>>>'+ startDT2); **output**=2014-12-09 8:00:00
String myDate = startDT2.format('M/d/yyyy h:mm a');
system.debug('select mydate>>>'+ myDate); **output**=12/9/2014 1:00 AM
Datetime dt = Datetime.valueOf(myDate);
system.debug('date_string >>>'+ dt); **output**=invalid date/time
How to solve my problem?
From the documentation of Datetime.valueOf():
The specified string should use the standard date format “yyyy-MM-dd
HH:mm:ss” in the local time zone.
You can't use 12/9/2014 1:00 AM string and convert it to Datetime.
If the users locale is English (United States) then you can use the DateTime.parse(string) method:
Constructs a Datetime from the String datetime in the local time zone and in the format of the user locale.
Example
This example uses parse to create a Datetime from a date passed in as a string and that is formatted for the English (United States) locale. You may need to change the format of the date string if you have a different locale.
Datetime dt = DateTime.parse('10/14/2011 11:46 AM');
String myDtString = dt.format();
system.assertEquals(myDtString, '10/14/2011 11:46 AM');
Incidentally, the Salesforce StackExchange site is a great place to ask Salesforce specific questions.
I am trying to retrieve DateTime data type data from database in Asp.Net. Here is the code:
DateTime date = DateTime.Parse(dr["packingDate"].ToString());
However, the problem is the date I retrieved display in such format : 26/11/2013 12.00 AM . I just wanted to display the date but not the time. I'd tried to do this:
DateTime date = DateTime.Parse(dr["packingDate"].ToString());
string dateStr = date.ToString("dd/M/yyyy", CultureInfo.InvariantCulture);
However, it does not works in my grid view. I wonder why is it so?
Thanks in advance.
Use the built-in to string function for jsut the Date value...
DateTime.ToShortDateString();
Select the corect type from database, so in this case datetime. Then you get a DateTime in your DataTable which you can cast with the DataRow's Field extension method:
DateTime date = dr.Field<DateTime>("packingDate");
If you only want to diplay the date:
string dateOnly = dr.Field<DateTime>("packingDate").ToShortDateString();
or
string dateOnly = dr.Field<DateTime>("packingDate").ToString("d");
or
string dateOnly = dr.Field<DateTime>("packingDate").ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
You could format on the GridView control, for sample:
<asp:BoundField DataField="packingDate" HeaderText="PackingDate"
DataFormatString="{0:dd-M-yyyy}" />
I have a string that holds a value in the format of ddMMyyhhmmss.
Example 240512024707
I need to be able to convert this date to a real .NET Date object.
I am currently using CDate but it seems CDate does not recognize the format, is there any way of specifying the string format to CDate ???
row.Item("NoteDate") = CDate(n.noteText.Substring(0, 12).ToString).ToString("dd/MM/yyyy hh:mm:ss")
You have the string with the date, you know the exact format string - use DateTime.ParseExact (or DateTime.TryParseExact if you wish to avoid the potential exception being thrown):
DateTime.ParseExact("240512024707", "ddMMyyhhmmss", CultureInfo.InvariantCulture)
Just wondering how I can convert the following string into a datetime 111222, at the moment it is telling me that this is not a valid datetime value..
You can use the DateTime.ParseExact method and supply the format your input is in.
Edit - added code:
DateTime dt = DateTime.ParseExact("111222", "yyMMdd", CultureInfo.InvariantCulture);
Console.WriteLine(dt.ToString());
My jquery function returning date in json format,so i want to know how to convert this into datetime formate "mm/dd/yyyy"
DateTime date1;
DateTime.TryParseExact(formCollection["date"], "MM/dd/yyyy", new CultureInfo("en-US"), DateTimeStyles.None, out date1);
it will not throw any exception.
if there is wrong format, you can compare with DateTime.MinValue, that is it succesfully converted.
How do I format a Microsoft JSON date?
You can use this to get a date from json:
var date = eval(jsonDate.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
and then you can use JavaScript Date Format script (1.2 KB when minified and gzipped) to display it as you want.
There is no date data type in JSON, so what you have is a date formatted as a string. Use the ParseExact method to parse the string into a DateTime value.
Then you can use the ToString method or String.Format method using the pattern MM'/'dd'/'yyyy to format the DateTime value into a string.