I have a column with a string representing a datetime, where the month name is 3 letters, and there's the time zone info. How do I convert it into a datetime?
"Jul 13 2020 23:05:58 GMT" --> 2020-07-13T23:05:58.000
This will create a table with a single string column, with a single sample record (for the sake of answering your question)
.set Ts <| print s = "Jul 13 2020 23:05:58 GMT"
This will convert each string value in that column/table from string to datetime
Ts
| extend dt = todatetime(s)
relevant docs:
todatetime()
supported datetime formats
Related
How can I to convert from Indian standard time format to oracle date format.
Eg:
Mon May 23 2016 00:00:00 GMT+0530 (India Standard Time)
required format
23-May-16
This parses the input string and returns a date value to you:
select cast(to_timestamp_tz('Mon May 23 2016 00:00:00 GMT+0530', 'Dy Mon DD YYYY HH24:MI:SS "GMT"TZHTZM') as date) as converted_to_date_value
from dual;
This parses the input string to a "timestamp with time zone" value and formats the value back to a string in your desired format:
select to_char(to_timestamp_tz('Mon May 23 2016 00:00:00 GMT+0530', 'Dy Mon DD YYYY HH24:MI:SS "GMT"TZHTZM'), 'DD-Mon-RR') as converted_to_your_format
from dual;
Enjoy!
Footnote: Please note that there's no such thing as "oracle date format" which you refer to. Oracle has its date data type, which can have many many many different display intepretations depending on your client as well as server locale settings.
I have a date value as Fri Feb 15 19:43:05 EST 2013
I could convert it into 2013-02-15 07:43:05 as String.
Now i need to get the date object of this String. I tried using simpleDateFormat in Groovy/Grails but it would just return the original value: Fri Feb 15 07:43:05 EST 2013
The way I am doing it is:
String dateCreated = dateValue.format("yyyy-MM-dd hh:mm:ss")
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") //required format
Date newDate = sdf.parse(dateCreated)
Can anyone help me?
If you want dates without Time Zone I suggest you to look the Joda-Time API. There's a plugin for Grails.
Look at LocalDate and LocalDateTime, you can construct them passing separated values (year, month, day, hour, second).
If you have a Date instance, you just need:
Date dateValue = ...
String dateCreated = dateValue.format("yyyy-MM-dd hh:mm:ss")
and dateCreated will contain value like 2013-02-15 07:43:05. You don't need to do anything else
I am using JodaTime2 library to create a date object with a given timezone as follow:
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
DateTimeZone tz = DateTimeZone.forID("America/New_York");
System.out.println("timezone=" + tz);
Date d = new DateTime(2013, 1, 1, 0, 0, tz).toDate();
System.out.println("Cur Date = " + d);
However when I print this date, the timezone reported is CST. What am I missing ?
timezone=America/New_York
Cur Date = Tue Jan 01 13:00:00 CST 2013
You're printing out the value of a Date object. Date doesn't have a time zone - Date.toString() always just uses the "default" time zone. A Date is just a number of milliseconds since the Unix epoch; it doesn't know about calendars or time zones.
You should either just stick within the Joda Time world, or (if you must) use a SimpleDateFormatter to convert a Date to a String - you can set the time zone on the formatter.
I am using the following function to Convert a String variable to a Date. Subtract a day from it and convert the date back to a String. The code goes as follows
Dim edate As String
Dim expenddt As Date
edate = txtenddt.Text
expenddt = Date.ParseExact(edate, "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo)
expenddt = expenddt.AddDays(-1)
Dim asd As String = expenddt.ToString
If edate has a value 29/12/2011 than the value in expenddt gets changed to a different format and the value in expenddt comes to 12/29/2011 and later after subtracting a day expenddt is 12/28/2011 and than when i convert it back to a String i get the value in asd as "12/28/2012 12:00:00 AM"
I have changed the date format on my system to d/M/yyyy in Regional And Language Option in Control Panel but i still get a different format in expenddt
Can anyone explain me why this is happening? How can i keep the format of the date in dd/mm/yyyy e.g 29/12/2011 and after
Subtracting a day it should remain 28/12/2011 and not 12/29/2011
Use Date.ToString() to convert date to string with dd/MM/yyyy format.
Dim asd As String = expenddt.ToString("dd/MM/yyyy")
Just try this:
Dim dateString As String = "Mon 16 Jun 8:30 AM 2008"
Dim format As String = "ddd dd MMM h:mm tt yyyy"
Dim dateTime__1 As DateTime = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture)
hope this may helpful....
I'm trying to convert a string into a date format
My string looks like this
Dim MyString as String = "June 2011"
And I'm trying to convert it like this
Convert.ToDateTime(MyString).ToString("MM yyyy")
But it's giving me the error
Syntax error converting datetime from
character string.
The desired output would be either 06/2011 or 01/06/2011. The value of my string will only ever have the month and year in though.
Any ideas?
A datetime object in .NET represents one single point in time. To create one from a Gregorian calendar date, you need all three parts (day, month, year), otherwise it can't represent one single point in time.
Since your business requirement is to assume that the day number is 1 if not provided, just insert that number into the string before parsing.
Dim myDate As Date = Convert.ToDateTime("1 " & MyString)
EDIT:
Sorry, forgot to mention that string manipulations like that are of course culture-dependent. You don't mention what culture you are in. "June 2011" can imply either en-GB or en-US. Since inserting the day number at the start of the string is easier than trying to insert it between the month and year, I suggest you go for something like this.
Dim myDate1 As Date = Date.Parse("1 " & myString, CultureInfo.GetCultureInfo("en-GB"), DateTimeStyles.AllowWhiteSpaces)
Convert.ToDateTime() only takes the full date so try prepending 01 as the day first i.e.
Convert.ToDateTime("01 " + MyString)
DateTime.Parse(MyString).ToString("MM yyyy")
This worked for me
Dim provider As Globalization.CultureInfo = Globalization.CultureInfo.InvariantCulture
Dim MyString As String
Dim d As DateTime = DateTime.Now 'test start
'test all Month Year strings
For x As Integer = 1 To 12
MyString = d.ToString("MMMM yyyy") 'convert date to March 2011 then April 2011 then...
Dim dt As DateTime = DateTime.ParseExact(MyString, "MMMM yyyy", provider)
Debug.WriteLine(MyString & " " & dt.ToString)
d = d.AddMonths(1)
Next