Access - string to date - date + time - ms-access

I have a table t_am_chat and from this table I use column Date_creat --> Format String in this format 2020-01-01T02:39:45
Goal is transfer to date type DateTime, ideal in format yyyy.mm.dd hh:mm:ss
Any tips please? Thanks.

Vasku, try this:
CDate(Replace(Date_creat,"T"," "))
To test it put copy this to Immediate window of VBE:
?CDate(Replace("2020-01-01T02:39:45","T"," " ))
After hitting Enter you should get this result:
1/1/2020 2:39:45 AM

Related

Postgres conver json string to date format

I am pulling the close date from a json field using:
data->>'closeDate' as PortalCloseDate however this returns a string and I am trying to convert it to a date format. The date format in the string is YYYY-MM-DD so I tried (data->>'closeDate')::date but am getting the error: "ERROR: invalid input syntax for type date: "‎2020-‎12-‎09"" Any help is appreciated, thanks!
Try bruteforcing on cleaning and casting the string to date:
select
cast(replace(cast(data->>'closeDate' as varchar), '"','') as date) as "closeDate"
from table

How can I get the timestamp in “yyyy/MM/dd hh:mm:ss.fff"” format in VBA?

I want to get this output:
2018-09-02 00:00:00.000
I tried the below code:
.Cells(LRS + 1, 15).Value = Format(.Cells(LRS + 1, "A").Value, "yyyy-MM-dd hh:mm:ss.fff")
And I got:
2018-09-02 00:00:00.fff
The initial date in Excel has the following format yyyy/mm/dd, no time included. That's why the time part includes only zeros 00:00:00.000. The reason I want to include the time in the specific format is that I'm planning to import those dates into a SQL table with that format.
Is there any solution?
As you can see from the documentation fff is not recognised as a formatting token in VBA.
Helpfully, you can actually import your data into SQL without formatting it to add the time. If you import it into a datetime field the SQL engine will automatically default the time part of the field to midnight on the date you give it.
I think you can just change your format string to yyyy-MM-dd by itself.
However if you really want to do it like this, then since there's no time specified then just hard-code 000 instead of fff. The rest of the time can be similarly hard-coded, since it never varies, so you end up with yyyy-MM-dd 00:00:00.000. But as I said, I think it's a bit pointless.
After replacing the cell format with the corresponding format, it is likely that the value of the cell is imported as text, not as a value.
Sub test()
Dim s As String, s1 As String, s2 As String
'First Cell format as your "yyyy-mm-dd hh:mm:ss.000"
Range("a2").NumberFormatLocal = "yyyy-mm-dd hh:mm:ss.000"
'In vb,This format("yyyy-mm-dd hh:mm:ss.000") is not recognized.
Range("b2") = Format(Range("a2"), "yyyy-mm-dd hh:mm:ss.000")
s1 = Format(Range("a2"), "yyyy-mm-dd hh:mm:ss.000")
's1 = "2018-09-03 01:24:33.000"
'Since you format a2 cell as "yyyy-mm-dd hh:mm:ss.000" you can get data as text
Range("b2") = Range("a2").Text
s2 = Range("a2").Text
's2= "2018-09-03 01:24:33.240"
End Sub
Sheet Data
Local Window

Inserting date in Mysql (codename one)

I want to insert a Date object in mysql database, which has a Date type in the database as well. I am having problems inserting the date .
I have tried this code, but it seems codename one have a problem with it:
dateString s;
s = date.getCurrentMonth() + "/" + date.getCurrentDay() + "/" + date.getCurrentYear();
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date startDate = (Date) formatter.parse(s);
Please can you tell me how to do it ?
You don't need to format it. Just use this SQL Date Object instead of Date object from java.util package.
import java.sql.Date
// Creating a date object.
Date date = new Date();
In a database, make sure the data type of attribute 'date' is selected as "Date" also, not VarChar. Simply pass this sql package Date object into the database through query. :) It will save the date in a format.

User defined date format in VBA Access

I can't figure out how to tell VBA the correct date format, as it seems to assume in the wrong way around. So CDate("24/02/2016 10:59") is giving the correct date - dd/mm/yyy. However when it iterates through a date like CDate("01/03/2016 00:59") - it assumes number 03 is not a month but a day so VBA is assuming it's mm/dd/yyyy, so it's giving me a date 03/01/2016 which is wrong and messing my data. So how can I tell VBA it's actually dd/mm/yyyy format. VBA seems to automatically pick up nicely even if it's "2016/01/14", but it's failing when it's not really obvious which part of numbers are months e.g. both dd and mmare less than 12.
I'm reading this date from a CSV file using WS.Cells(irow, icol).Value.
This is what I tried so far:
datDate = CDate(Format(WS.Cells(iRow, iCell).Value, "dd-mmm-yyyy hh:mm:ss"))
When the CDate() function encounters an ambiguous "aa/bb/yyyy" date string it will be interpreted according to the order that the Month and Day appear in the short date format defined by the Regional Settings in Windows.
For example:
When my machine is set to "English (United States)" with a short date format of M/d/yyyy then Month(CDate("02/04/2016")) returns 2.
However, after changing my Regional Settings to "English (Canada)" with a short date format of dd/MM/yyyy then Month(CDate("02/04/2016")) returns 4.
Note that this is different from the behaviour of the Jet/ACE database engine when interpreting ambiguous #aa/bb/yyyy# date literals in SQL statements. In that case it will always interpret them as mm/dd/yyyy regardless of the regional settings.
The best solution, as always, is to ensure that the string representation uses an UNambiguous date format, e.g., 2016/02/04 will always be interpreted as Feb 4.
Use mm/dd/yyyy hh:nn:ss like:
#03/01/2016 16:32:58#
or DateSerial and TimeSerial:
DateSerial(2016, 03, 01) + TimeSerial(16, 32, 58)
To parse the string, use Mid:
TrueDate = Dateserial(Mid(s, 7, 4), Mid(s, 4, 2), Mid(s, 1, 2)) + TimeSerial(Mid(s, 12, 2), Mid(s, 15, 2), 0)
s = "01/03/2016 00:59"
' Returns: 2016-03-01 00:59:00
s = "24/02/2016 10:59"
' Returns: 2016-02-24 10:59:00
Change the format of the date before you convert. Something like this;
Dim strDate As String, strFormatDate As String
Dim dte As Date
strDate = "01/03/2016 00:59"
strFormatDate = Format(strDate, "dd-mmm-yyyy hh:nn")
dte = CDate(strFormatDate)
This changes the date string to 01-Mar-2016 00:59 which should convert to a date data type without confusion.

How to deserialize date format in JSON date string?

This question someone is already asking and solution is with JavaScriptConverter (.NET) but how can I convert normal date into JSON date string with java script.
For example I have a formated date "12-12-2012" and I want to get string something like this example:
/Date(1354316400000+0100)/
Icky, awful format, and clumsy slow serializer. (IMHO)
On the server, use Json.Net and its default ISO8601 formatted dates instead.
On the client, use moment.js. It will handle all of the parsing and formatting you could want.
For posterity, if you want to output this format using moment.js, you can do one of these:
moment().format("[/Date](XSSS)/"); // /Date(1198908717056)/
moment().format("[/Date](XSSSZZ)/"); // /Date(1198908717056-0700)/
s = "12-12-2012".split("-");
epoch = Date.parse(s[2] + "-" + s[0] + "-" + s[1]);
output = "/Date(" + epoch + ")/";
if you need the timezone offset, you can use .getTimezoneOffset() on the Date Object and add that to your output string.