I'm trying to make this line work:
MyDateTime = DateTime.ParseExact("2/8/2013 11:59:00 AM", "yyyy-mm-dd hh:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo)
I'm getting date/time strings from spreadsheets in the above format, and I can't control that. There's a ton of help online, including this site, about converting strings to dates, and I've tried them all, but I keep getting this error :
"System.FormatException: String was not recognized as a valid DateTime."
I"m just about ready to write my own custom parser, but that doesn't seem very elegant. Is there some built-in way to convert a string like mine into the date/time format I need?
Thanks for any help.
Your format string is wrong. You're entering a date in d/M/yyyy hh:mm:ss tt format, but telling it to expect a date in yyyy-mm-dd hh:mm:ss format. The two do not match exactly, so DateTime.ParseExact is quite rightly throwing an exception at you.
Try:
MyDateTime = DateTime.ParseExact("2/8/2013 11:59:00 AM", "d/M/yyyy hh:mm:ss tt", System.Globalization.DateTimeFormatInfo.InvariantInfo)
This tells it to expect the following characters:
Integer from 1 through 31 (depending on the length of the month)
/ character
Integer from 1 through 12
/ character
4 digit year
Space
Integer from 1 through 12
: character
Integer from 00 through 59
: character
Integer from 00 through 59
Space
Two character meridian specifier ("AM" or "PM")
For more info on the datetime format strings, check out this MSDN page
I think you need to change the string format to match what you are passing in. You seem to be passing in something more like this:
"d/M/yyyy hh:mm:ss"
Give that a try and see how it works. Note that you need to use MM for months--'mm' is used for minutes.
Related
I tried to convert via:
TO_TIMESTAMP ("T00:00:00", '"T"hh:mi:ss')
It displayed error>>
ORA-01861: literal does not match format string
01861. 00000 - "literal does not match format string"
*Cause: Literals in the input must be the same length as literals in
the format string (with the exception of leading whitespace). If the
"FX" modifier has been toggled on, the literal must match exactly,
with no extra whitespace.
*Action: Correct the format string to match the literal.
Your immediate issue is with hh: this stands for a 12-hour based format (am/pm), so it allows values between 1 and 12 only (0 is not a valid hour in 12-hour format).
You also should not be using double quotes around the literal string ; but I don't think you are actually doing that in your code, otherwise you would get error invalid identifier.
Then: there is no time datatype in Oracle anyway - just date (which, couter-intuitively enough, includes a time portion), and various flavors of timestamps. If you are looking to convert the input string to a string that represents a time in AM/PM format, then you can do something like this:
to_char(to_date('T00:00:00', '"T"hh24:mi:ss'), 'hh12:mi:ss am')
This turns the time string to a date (the date part defaults to the current date when not specified), then returns its time portion format in am/pm format. This yields:
12:00:00 AM
I have issue to convert ISO date time to local datetime.
the flowing code is not work with me and give error
Dim txt As String = "20200530T015253+08"
Dim output As DateTime = DateTime.ParseExact(txt, "u", System.Globalization.CultureInfo.InvariantCulture)
the error is
System.FormatException: 'String was not recognized as a valid DateTime.'
Your date string is not an "ISO date" as far as I can see. ISO 8601 would lay it out like "2020-05-30T01:52:53+08:00" and .net would parse it with "o"
Parse it by specifying the format:
DateTime.ParseExact("20200530T015253+08", "yyyyMMddTHHmmsszz", Nothing)
If your input data will vary its presentation for time zones that have a fractional hours component such as India being 5.5 hours, then you'll need to vary your format string to zzz
I know the question is really asked often, but I can't find the solution to my problem. My code is
Public Function ConvertFacebookDateToNETDate(Instant As String, Format As String) As Date
'Dim UTCOffset As New Integer
'UTCOffset = Instant.Substring(Instant.IndexOf("UTC+") + 4, 2)
Dim MyDateTime As DateTime
MyDateTime = New DateTime()
MyDateTime = DateTime.ParseExact(Instant, Format, CultureInfo.InvariantCulture)
'MyDateTime = MyDateTime.AddHours(-1 * UTCOffset)
Return MyDateTime
End Function
ConvertFacebookDateToNETDate("Friday, May 9, 2014 at 9:48am UTC+02", "dddd, MMMM d, yyyy at h:mtt UTCK")
What is going wrong here ?
Thanks
Two things (all the info comes from there):
In your format you put "at" but "t" if a format specifier so you have to escape it "a\t" or put it between literal delimiter "'at'"
The "K" format specifier for a DateTime with Kind local (a "±XX") needs to be "±XX:XX" so you have to either pass a Date string with that pattern or use "zz" format specifier instead of "K"
There are two problems with the data format
The t in at is interpreted as the short form of tt, i.e. the first character of AM/PM designator. You need to escape it to specify that it's a literal character.
The specifier for time zone offset in hours is z, not K.
So:
ConvertFacebookDateToNETDate("Friday, May 9, 2014 at 9:48am UTC+02", "dddd, MMMM d, yyyy' at 'h:mtt UTCz")
I'm using beanshell and I need to convert a string in the format '2012.08.14 07:30:00.000' to date time integer in the format 1344925800000
Does anyone have any ideas?
Thanks in advance
Al
I hope that your example result is only an example, because using Java means I obtain a different result. But a standard is to use the number of milliseconds since January 1, 1970, 00:00:00 GMT.
I don't know if there is a more generic way, that would allow you to specify a format string. Here I use a string conversion and a format for specific locale.
import java.text.DateFormat;
sDate = "2012.08.14 07:30:00.000";
// replace beginning dots with hyphens
sDate = sDate.replaceFirst("([0-9]{4})\\.([0-9]{2})\\.([0-9]{2}) ", "$1-$2-$3 ");
// use a date format from a specified locale
formatter = DateFormat.getDateInstance(
DateFormat.MEDIUM,
new Locale("PL"));
javax.swing.JOptionPane.showMessageDialog(null,
"time=" + formatter.parse(sDate).getTime());
I followed suggestion from vb.net convert string to date . But, it did not work.
the code is as follows:
Dim Dt As DateTime
If DateTime.TryParse("Thu, 09 Dec 2010 16:03:24 EST", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, Dt) Then
MessageBox.Show(Dt)
End If
Can anyone solve this for me? I need to have date populated in the format of "yyyy-mm-dd hh24-mi-ss".
this should get you on the right path:
Dim dt As DateTime
dt = Now
TextBox1.Text = Format(dt, "yyyy-MM-dd HH:mm:ss")
NOTE: this answer was written for a previous revision of the question which had the following code:
Dim Dt As DateTime
If DateTime.TryParseExact("Thu, 09 Dec 2010 16:03:24 EST", "dd.MM.yyyy", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, Dt) Then
MessageBox.Show(Dt)
End If
The format string that TryParseExact takes as it's second parameter specifies the format of the date in the string passed as the first.
In your case the format string is specifying that the date will be of the format "09.12.2010" for example - just the day, moth and year. However, as the string isn't in that format it won't parse. If you'd just used ParseExact it would have raised an exception.
The MSDN page for the variant of TryParseExact that takes an array of possible format strings has more examples, but non match your format exactly, but working with the format strings used to convert DateTime to string you probably want something like this:
"ddd, dd MMM yyyy HH:mm:ss ???"
but I can't find what you'd need instead of "???" to match the "time zone as string". You might have to do some string manipulation to remove this before calling TryParse or TryParseExact.
You will have to replace the timezone with the timezone offset.
Same question as Parse DateTime with time zone of form PST/CEST/UTC/etc
TextBox1.Text = System.DateTime.Now.AddMinutes(698).ToString("dd/MM/yyyy");
DateTime DOB;
string[] formatsDOB = { "dd/MM/yyyy", "MM/dd/yyyy" };
DateTime.TryParseExact(txtDateofBirth.Text, formatsDOB, CultureInfo.CurrentCulture, DateTimeStyles.None, out DOB);