I am parsing a date from a bank deposit report, and the format is like this:
Jul 9 2015
Jun 20 2015
Basically MMM dd yyyy except that the single digit day does not contain a leading zero. Is there a simple way to do conditional formatting in DateTime.ParseExact()? Or will I have to pre-process the date string and either add the leading zero or remove the extra space?? Here is what works for the single digit day dates:
Dim dtDepositDate As DateTime
dtDepositDate = DateTime.ParseExact(strDate, "MMM d yyyy", CultureInfo.InvariantCulture)
and obviously, MMM dd yyyy would work for the two digit dates, but would not work for the single digit dates with an extra space in between.
For single/double digit day part
Use single d which is good for both single and double digits day value.
Having single d would effect the values if the DateTime is converted to string. As far as parsing is concerned, it will work for both single and double digits day values, like 01 , 1 , 11, 20 etc. The same is true for M, H, m, specifier for Month, Hour Minutes etc.
For multiple spaces
For multiple spaces use DateTimeStyles.AllowWhiteSpaces in parsing.
DateTime dt = DateTime.ParseExact("Jul 9 2015", "MMM d yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.AllowWhiteSpaces);
or for double digit day part:
DateTime dt = DateTime.ParseExact("Jun 20 2015", "MMM d yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.AllowWhiteSpaces);
Related
I need to convert string to Timestamp.
The problem is that the input is coming from a csv file and contains date-time values such as:
Mar 3 2022 8:30AM
Apr 27 2022 7:37AM
If I use the following conversion:
to_timestamp(to_timestamp(trim(DateColumn), 'MMM dd yyyy h:mma'), 'yyyy-MM-dd HH:mm:ss')
It converts the date Apr 27 2022 7:37AM correctly, but throws error while converting Mar 3 2022 8:30AM because of the extra space between the Month and Date values and that the date 3 is not 03.
Is there a way to convert these 2 strings formats into Datetime?
It is recommended that you first uniformly replace multiple spaces with a single space, and then convert to timestamp.
val df1 = df.withColumn("ts", to_timestamp(regexp_replace(trim(col("ts")), "\\s+", " "), "MMM d y h:mma"))
I have varchar like this "Wed Jan 26 2022"
I need to convert this to date in sql. How can i do this
for Sql Server:
convert(date, substring('Wed Jan 26 2022',5,11),9)
we ignore the Day name (superfluous), and convert the rest using format 9 indicating Mon dd yyyy format.
SQL*plus server (Here's my code) -
If I am right, you want such type of string which is an invalid one to convert that into a valid one so that you can store valid data into the database. then this code you can use->
SELECT TO_DATE('WED JAN 26 2022','DY MON DD YYYY')FROM DUAL;
(Explanation)->
Code will convert invalid date datatype to a valid date data type which is used in Oracle(SQL).
DY = Abbreviated Week Day
DD = Month day indicator
MON = Abbreviated month
YYYY = Four-digit year indicator
When reading DateTime from excel, the result day and month are being read as dd MM, while the excel content was according to MM dd style.
S1 contains: "12/09/2017"
The code:
Dim t_from As DateTime
t_from = CDate(s1)
t_from includes Sep as a month, instead of Dec as should be.
I also tried:
Dim b As Boolean = DateTime.TryParseExact(s1, "MM/dd/yy",
System.Globalization.CultureInfo.InvariantCulture,
Globalization.DateTimeStyles.None,dt)
this code fails (String was not recognized as a valid DateTime)
How can I convert the text to DateTime VB variable, according to month first (before date)?
Format "M/d/yyyy h:m:s tt" should parse correctly dates from the excel
Dim parsedDate As Date
Date.TryParseExact(
"9/12/2017 12:00:00 AM",
"M/d/yyyy h:m:s tt",
Globalization.CultureInfo.InvariantCulture,
Globalization.DateTimeStyles.None,
parsedDate)
parsedDate.ToString() ' 12.09.2017 00:00:00
For "12/09/2017" where 12 is a month and 09 is a day use format: "MM/dd/yyyy"
I have this string: 28 June 2018 (22:05)
How can I compare it with my current time and get the difference?
For example if actual time was 29/06/2018 (05:49)
The difference will be: 7 hours 44 minutes
So input: 28 June 2018 (22:05)
Output: 7 hours 44 minutes
The first thing you need to do, is convert the string to a valid DateTime instance.
If you know your dates will always be in this format, you can do the following...
Dim mydate = DateTime.ParseExact("28 June 2018 (22:05)", "dd MMMM yyyy (HH:mm)", CultureInfo.InvariantCulture)
https://msdn.microsoft.com/en-us/library/w2sa9yss(v=vs.110).aspx
Once you've parsed the string into a valid DateTime instance, you can use all the normal date functions to do the comparisons.
I would first get the difference in minutes, like so...
Dim diffminutes = DateDiff(DateInterval.Minute, mydate, Now)
Then create a timespan like this...
Dim mytimespan = TimeSpan.FromMinutes(diffminutes)
Finally display the difference in hours and minutes like this...
Response.Write(mytimespan.ToString("hh\:mm"))
How can i convert the table containing date in text format DY Mon DD YYYY into DD MM YY date format which can be used for sorting when exported to a spread sheet.
The first date format is "Sun Jan 6 2013" which needs to be converted to "06-Jan-13" to be in date format not text which can be sorted on spread sheet and the type of the column is Text.
Thanks in advance.
You can convert the text you have to a DATE value, and then re-format it:
SELECT TO_CHAR(TO_DATE(date_column, 'DY MON DD YYYY'), 'DD MM YY')
FROM my_table