I have a string 7/24/2013 6:05:00 PM and want to convert it to a DateTime object.
I am using
DateTime newDate = DateTime.ParseExact(date,"M/d/yyyy h:mm:ss tt",
System.Globalization.CultureInfo.InvariantCulture);
but the newDate object is being 09.07.2013 06:45:00. I want it as it is seen above the string version.
Do you have any idea why it is not converted the format I wanted or any opinion would be great how I can render it as a datetime object.
Thank you
As your date is already in a common format style you should try parsing using the current UI culture, in your case en-US.
E.g.
DateTime.ParseExact(date,"M/d/yyyy h:mm:ss tt", System.Globalization.CultureInfo.CurrentCulture);
By using the InvariantCulture the parsing is ignoring all cultural clues whilst parsing your string.
For more information: http://msdn.microsoft.com/en-us/library/dd465121.aspx
Related
Getting Error
java.text.ParseException: Unparseable date: "2018-09-09T09:04:47+00:00"
Whatis the date format is this?
There seems to be some confusion, about this date format, so I will explain.
This is an ISO 8601 date-time value with a timezone offset.
References:
The ISO 8601 page in Wikipedia is accurate and pretty comprehensive.
The W3 Consortium have documented in a TR; here.
The Official version may be obtained from ISO; here.
The Z is an ISO standard timezone specifier. It means the same thing as +00.00, and is known in some circles (e.g. the military) as "Zulu time".
An ISO 8601 date/time does not "have to" end with a Z. There are other forms of timezone specification, and indeed a date/time does not need a timezone at all.
You should not need to trim it off the Z. Java's data-time parsers can parse the Z timezone specifier and give it its correct meaning ... if you use the right pattern.
If an ISO date time is "unparseable", that means that you have (explicitly or implicitly) used the wrong format to parse it.
Unfortunately, different countries (locales) have different default date / time formats, and worse still there is no reliable way (in general) to know which is the correct one to use ... if you don't know where it came from.
Fortunately ... ISO 8601 is an international standard. If you see a date / time that conforms to the ISO 8601 syntax, you know what it means.
There are a number of kinds of ISO 8601 date and date/time representation as explained in the W3 Consortium TR. The different kinds can be distinguished without any ambiguity.
If you are parsing using java.text.SimpleDateFormat, then the correct pattern for this variation of ISO 8601 is "yyyy-MM-dd'T'HH:mm:ssX". The pattern that works with java.time.format.DateTimeFormatter is also "yyyy-MM-dd'T'HH:mm:ssX"
The DateTimeFormatter class also defines a number of standard formats as constants. The format for this kind of ISO 8601 date/time is DateTimeFormatter.ISO_OFFSET_DATE_TIME. The other kinds are defined too.
The Date and Calendar classes and associated classes are legacy classes. It is advisable to use the new java.time classes instead in new code.
You should use 'X' for timezone:
String pattern = "yyyy-MM-dd'T'HH:mm:ssX";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
Date date = simpleDateFormat.parse("2018-09-09T09:04:47+00:00");
System.out.println(date);
Use this convert method to String to date which is ISO format. But in generaly ISO format ends with Z , but if not no problem. This method returns Date, and do it what ever u want from this return value.
public static Date toCalendar( String isoDate)
throws ParseException {
String s = isoDate.replace("Z", "+00:00");
try {
s = s.substring(0, 22) + s.substring(23); // to get rid of the ":"
} catch (IndexOutOfBoundsException e) {
throw new ParseException("Invalid length", 0);
}
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(s);
}
In Java8 TimeApi or later version has this usage,try it ;
OffsetDateTime offsetDateTime = OffsetDateTime.parse( "2018-09-09T09:04:47+01:00" );
Instant instant = offsetDateTime.toInstant();
java.util.Date date = java.util.Date.from( instant );
Or use only this pattern yyyy-MM-dd'T'HH:mm:ssX
How can I convert a string in an iso date format like
"2015-06-28T03:39:43.176Z"
to look like
"2015-06-28 03:39:43 PDT" using Freemarker?
I tried some things like:
${x.Start?datetime(iso)?datetime}
${x.Start?datetime.iso?datetime}
But that didn't work.
You have to use the string builting to convert the datetime variable, created from your string with the datetime.iso built-in. ${"2015-06-28T03:39:43.176Z"?datetime.iso?string("yyyy-MM-dd HH:mm:ss zzz")} works for me.
I'm using joda time to format my ISO Date input string, but I'm getting an exception that my ISO Date is malformed:
Invalid format: "2014-06-20T11:41:08+02:00" is malformed at "+02:00"
This is my code:
val formatter: DateTimeFormatter = ISODateTimeFormat.dateTime.withZone(DateTimeZone.getDefault)
val date: DateTime = formatter.parseDateTime("2014-06-20T11:41:08+02:00")
What's wrong here?
The error comment is slightly misleading here, as Joda formatter you derive from ISODateTimeFormat expects the millisecond part of the date/time string to be present, therefore the following will work fine:
val formatter: DateTimeFormatter = ISODateTimeFormat.dateTime().withZone(DateTimeZone.getDefault())
val date: DateTime = formatter.parseDateTime("2014-06-20T11:41:08.0+02:00")
The answer by Radyk is correct.
ISO 8601 Formats Built-In
However, you needn't specify a formatter at all. The DateTime class has a built-in parser for your ISO 8601 compliant format, used automatically by the constructor.
DateTime dateTime = new DateTime( "2014-06-20T11:41:08+02:00", timeZone );
While the second argument is optional, I suggest you assign a DateTimeZone object to be assigned to the DateTime if you know such a time zone. The input string has an offset-from-UTC, but a time zone is more than just an offset. A time zone includes rules for Daylight Saving Time and other anomalies. Use proper time zone names, never 3 or 4 letter codes like EST or IST.
Other Formats
You can apply many other formats:
Built-in ISO 8601 formatters
Built-in localized (short, medium, long, and full formats, Locale-sensitive)
Custom specified by you.
For example, if you want only the date portion without the time-of-day in your String representation, call ISODateTimeFormat.date() to access a built-in formatter.
Example code in Joda-Time 2.8.
String output = ISODateTimeFormat.date().print( dateTime ); // Format: yyyy-MM-dd
Search StackOverflow for hundreds of other Questions and Answers about formatting date-time values.
I have a date string in the format "2013-01-31T10:10:05.000Z". I want to convert this string to a Date object in extjs.
I have tried to use Ext.Date.parse("2013-01-31T10:10:05.000Z","Y-m-dTH:i:s.uZ"). But it is returning undefined.
I also tried with new Date("2013-01-31T10:10:05.000Z"), but it is also returning undefined.
Note: I have tried in IE8 browser.
Could anyone please help me to convert the above date string to Date object?
Thanks a lot sra. Now I am getting the result as ...UTC+5:30... Is there any way to convert this in IST format?
Try Ext.Date.parse("2013-01-31T10:10:05.000Z","c");
The c is the format type for ISO 8601 formatted dates
See the Ext.Date API for more details on this or other available formats
That's because 'T' and 'Z' are special characters in the Date format: http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.Date
You have to escape them like this: Ext.Date.parse("2013-01-31T10:10:05.000Z","Y-m-d\\TH:i:s.u\\Z")
i can get my datetime populated as 2012-07-24 15:31:21
However, its not in the format that i want. This is my current query
SELECT CONVERT(datetime2(0), convert(varchar(20),GETDATE() , 120))
i need the above displayed datetime in the format DD/MM/YYYY HH:MM:SS.
i have searched all over the place. Converting date format in T-SQL is available generally for datatype Varchar. Can someone advise me for datetime formatting via SQL directly?
thanks.
Either that, or you can specify the date/time format directly as the parameter of the ToString method:
string dateTime = DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss");
or second answer is
You can use any culture that supports the dd-mm-yyyy format like the french one.
For example to format the date time now in a format dd-mm-yyyy you can do as follows:
cultureInfo culture = new cultureinfo("fr-FR");
string oFormatedDate = dtNow.ToString("d", culture);