Convert datetime YYYYMMDD to DDMMYYYY via T-SQL directly - tsql

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);

Related

Converting string with format "2014-08-12-12:42:38:133936" to datetime

I have an issue converting this string coming from a web service into a datetime for our database.
I know I could take a substring of each side date / time and format the hell out of it so that i would essentially break it apart and add the time back to the date, but there has to be a more simple way. I am terrified if I do it the "hack" way then somewhere down the line there will be a date that will break the logic.
So does anyone have a tip in converting a string with the format "2014-08-12-12:42:38:133936" into a Datetime datatype?
If you don't need to need to keep the accuracy to yyyy-mm-dd hh:mm:ss.mmmmmm or you are happy with yyyy-mm-dd hh:mm:ss.mmm
This should do it:
DECLARE #value VARCHAR(MAX) = '2014-08-12-12:42:38:133936'
SELECT convert(datetime, STUFF(SUBSTRING(#value,1,LEN(#value)-3),11,1,' '), 21)
or
SELECT CAST(STUFF(SUBSTRING(#value,1,LEN(#value)-3),11,1,' ')AS DATETIME )

Convert ISO datetime string to standard date format in Freemarker

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.

joda time ISO DateTime formatting

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.

String can not be converted to a DateTime with ParseExact method

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

converting a utc format date string to date object in Extjs

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")