How to convert Date Time into String value? - asp.net

I try to Convert String into Date. I get the error such as Cannot implicitly convert type 'System.DateTime' to 'String'.
Aspx code
p.Dob = Convert.ToDateTime(txtDOB.Text);
Can you help me, How solve this error.

If "p.Dob" is string:
p.Dob = Convert.ToDateTime(txtDOB.Text).ToString("dd/MM/yyyy");

Related

Check if date valid using moment and format 'YYYY-MM-DDThh:mm:ss.SSS+ss:ss' does not work

I'm trying to check if a string YYYY-MM-DDThh:mm:ss.SSS+ss:ss
But I always get false.
For example:
const value = '2022-02-16T22:23:53.000+00:00'
moment(value, DATE_FORMAT, true).isValid();
Please advise, am I using the wrong format ?
Date format should have been - YYYY-MM-DDTHH:mm:ss.SSSZ

Getting error: Conversion from string "" to type 'Date' is not valid

If anyone can rewrite this for me I'd really appreciate it. I'm trying to change stored date if null. I've tried Me.vipEndDate ="0000-00-00" and much more.
If DR("vipEndDate") Is Nothing Then
Me.vipEndDate = "0"
Else
Me.vipEndDate = DR("vipEndDate").ToString
End If
Well you could add some error protection. The TryParse attempts to convert a string to a date, if it does not convert it returns false so you code will not execute with an error.
Dim dt As Date
If Date.TryParse(DR("vipEndDate").ToString, dt) Then
'this part only executes if string convert
Me.vipEndDate = dt
End If

Parse custom datetime in Pig

I have the following as string "2014-12-15 18:20:48" but when I parsed to datetime:
A = LOAD 'input.txt' AS (mydate:chararray);
B = FOREACH A GENERATE ToDate(mydate) as datetime;
I get this error: "2014-12-15 18:20:48" is malformed at " 18:20:48"
Alternatively, when I specify the format ToDate(mydate, 'yyyy-MM-dd HH:mm:ss'); the datetime is converted to "2014-12-15T18:20:48.000Z" instead of "2014-12-15 18:20:48". How can I resolve this to give me "2014-12-15 18:20:48" type datetime?
If we are passing string to ToDate function, the string should be of iosstring format(ref [1]).
ToDate(iosstring)
In the first case, the input string is not of iosstring format (T separator is missing, which signifies the beginning of time element). If the input string is
of iosstring format(ref [1]) it will work.
In second case, you are calling overloaded method of ToDate :
ToDate(userstring, format)
The return type of all ToDate method is DateTime object.
If you are getting a date in string and you need it in any other format for further processsing then you have to use ToString() method on top of ToDate() method to get it in the
desired format.
References :
iostring format : http://www.w3.org/TR/NOTE-datetime
ToDate(): http://pig.apache.org/docs/r0.12.0/func.html#to-date
ToString() : http://pig.apache.org/docs/r0.12.0/func.html#to-string

Issue in converting Timespan variable to 12 hour format

I have a nullable variable Start time
Timespan? st=e.StartTime;//Null-able variable;
I am trying to get time in AM/PM format but I am unable to get it.
DateTime date = Convert.ToDateTime(st.ToString());
String f = String.Format("{0:hh:mm:tt}", date);
Error is:
System.FormatException: String was not recognized as a valid DateTime.
If you were to output the results of st.ToString(), you will find that it doesn't contain any date information, only hours, minutes and seconds.
This isn't a valid format for a DateTime, which generally contain date and time information.
You don't need to convert your TimeSpan to a DateTime to format it, you can just use TimeSpan.ToString():
string f = st.Value.ToString(#"hh\:mm\:tt");
For reference: http://msdn.microsoft.com/en-us/library/ee372287.aspx
Also, note the \ before the :, you must do this if you want to include literal strings in the output, as mentioned at the bottom of that documentation page.
Converting a timespan to a date is not possible, a timespan represents x amount of minutes/hours/whatever and you cannot get an exact date from that alone. If you have a date as a starting point, you can add a timespan and that will give you the new date.
st.ToString() will return "System.Nullable<Timespan>" because that is what a nullable type returns - it does not override the default Object.ToString implementation, so returns the type name.
If you want the string of the actual timespan, then you would need to do st.Value.ToString(), but you should be checking for null first (i.e. st.HasValue == true)
Edit: Also see #Sean's comment about how to output the Timespan without converting to a DateTime first.
Edit: Turns out I was slightly wrong - st.ToString() doesn't return the above. So see Sean's answer.
First convert Timespan to Datetime by adding TimeSpan to a base date of 00:00 hrs. Then on that dateTime derive the 12 hr format.
DateTime.Now.Date.Add(OpenTimeSpan).ToString(#"hh\:mm\:tt")
The Accepted Answer is wrong.
You cannot return AM/PM for a TimeSpan because it is only concerned with the length of Time,
not a Time of Day - hench the name, "TimeSpan".
Convert to a DateTime first before converting to a String:
string sTimeOfDay = new DateTime().Add(st).ToString("hh:mm tt");
Note: If your TimeSpan is nullable, then you will need to add Conditional Logic to Handle Nulls and pass in ts.Value instead of ts:
string sTimeOfDay = (st == null ? null : new DateTime().Add(st.value).ToString("hh:mm tt") );

Flex: Converting DateField text to seconds?

Is there anything wrong with following snippet of code?
var d:Date = DateField.dateToString(myDateField.text,"DD/MM/YYYY");
testTextArea.text = d.getSeconds().toString();
Error: Implicit coercion of a value of
type String to an unrelated type Date.
Here is your problem: DateField.dateToString's first parameter is supposed to be a date. It then takes that date and returns a string using the second parameter as a format string.
It looks like you're trying to convert the string to a date (the other way around) so you can get the seconds from it and put it in the text area. The DateField control has a selectedDate parameter that will give you the date you need. Then you just run this code to put it in the text area:
testTextArea.text = myDateField.selectedDate.getSeconds().toString();

Resources