Date format in Go - datetime

I need to format a Date.Time object which is a UTC string into the following format “dd/mm/yyyy HH:MM:SS”. I need to loop through an array of transactions and alter the StatusDateTime for each transaction in the array.
I've tried the below while playing around with the format, but it does not alter the date format at all.
for _, Transaction := range Transactions {
Transaction.StatusDateTime.Format("2006-01-02T15:04:05")
}
What am I doing wrong?

There's a bit of confusion in this question. Let me break it down.
I need to format a Date.Time object which is a UTC string into the following format “dd/mm/yyyy HH:MM:SS”.
First, I think you mean a time.Time object. There's no such thing as a Date.Time object in Go.
Second, a time.Time object is, well, an object (well, a struct instance anyway). It is not a "UTC string". It's not a string at all! It's an arbitrary value stored in memory.
Now, you're on the right track by calling the Format method of time.Time. But as you can see by reading the Godoc for that method, it returns a string. Your code example ignores (and therefore discards) that return value.
You need to assign that value somewhere, then presumably do something with it:
for _, Transaction := range Transactions {
formatted := Transaction.StatusDateTime.Format("2006-01-02T15:04:05")
fmt.Println("the formatted time is", formatted)
/* Or store the formatted time somewhere, etc */
}
I've tried the below while playing around with the format, but it does not alter the date format at all.
Not to beat a dead horse here, but you're right, this doesn't alter the format at all... Or more accurately, time.Time has no format to alter in the first place.

Related

OUTPUT <NULL> WHEN SELECTING DATE FROM DATETIME - SQLITE

I'm new to the SQL world and im going crazy trying to figure out how to SELECT date from a datetime field in SQLITE.
Example: value <11/11/2005 14:56>, i just want to select <11/11/2005> for EVERY ROW.
I tried strftime(), date(), CAST() and other functions but the output its always NULL.
For example i tried querying SELECT strftime('%d/%m/%Y' , columnname) AS date FROM tablename;
OUTPUT: "NULL" in every row
Can someone help me understand what im doing wrong and how can i fix it? Thank you!!!
It always returns NULL because MM/DD/YYYY is not a valid sqlite date format. Treat the column as a string and use substr and instr to drop off the time portion. Something like (no guarantees, check the doc!)
SELECT substr(columname,0,instr(columnname,' '))
Re comment "how to order by the date in descending order"
This problem is a good argument (the best argument?) for storing the date in a sqlite date/time format. There is a strategy in this post for converting MM/DD/YYYY to YYYY-MM-DD (which sorts dates correctly).
If it's not too late, it would be advisable to change the date storage to a valid sqlite date format. strftime can be used to present the date as desired, and sorting will be accurate.

Sqlite get the date part of a DateTime and return as a DateTime, not a string

In Sqlite I want to extract the date and time portions of a DateTime field separately in a view and return them also as a datetime, not strings. I've tried Cast, Date(), datetime(), but they all return strings.
I've read the SQLite documentation and understand how there is not an actual Date data type. Yet a Table field defined as DateTime is able to be parsed as a Date by an Excel query, but calculations on that field are not. I'm trying to do all data prep in the database view.
My data has the following field taken directly from the table definition:
LastModifiedDate datetime
I want the date (without time) to have the same DateTime data type as LastModifiedDate, not Text, because I use this view in many spreadsheets. I can apply Excel Date functions and formatting to LastModifiedDate field directly as returned from the ODBC query to Excel, and want to do the same to the Date-only part. I don't want to have to put a string-to-date conversion in every spreadsheet when I know it can get the date natively from Sqlite in LastModifiedDate.
SELECT LastModifiedDate,
date(LastModifiedDate) as Datepart,
cast(LastModifiedDate as numeric) as Date2
FROM Transactions
LastModifiedDate Datepart Date2
2019-07-28 18:22:38.9165394 2019-07-28 2019
LastModifiedDate in the above query is interpreted in Excel as a date to which date formats and date functions can be applied with no further processing required. Datepart above is returned as Text to Excel, and I can't apply date functions and formats without further pre-processing in Excel. I would like Datepart to be interpreted a date in Excel just as LastModifiedDate is.
I'm looking at the ch-werner.de sqliteodbc-0.9998. It will return an ODBC TIMESTAMP type only if the column decltype starts with timestamp or datetime. It returns ODBC TIME only for decltypes starting with time and ODBC DATE only for decltypes starting with date.
sqlite3 provides this decltype only for result table columns that are direct database column references. So if your SELECT statement has some expression that is more than a plain column reference, the decltype is lost. sqlite3 works like this at least up to version 3.39.0. It is documented.
The CAST expression converts the value of given expression to a storage classes by the determined affinity of the given declared type, but does not assign decltype to the result.
If you want to see the decltypes for query columns, you can use the sqlite3 cli and give it command .stats 2. Then it'll output the column declared types for each statement it executes.
If the decltype is found, the sqliteodbc-0.9998 will always parse string values into ODBC types. If DSN Option JDConv is enabled, it'll also parse floating point julianday values (whether provided as float or a string of a float) into ODBC types and when writing it'll write floating point into database.
If you can afford to change the schema, you can add a generated virtual column. This is cheap in storage, because data is not affected, but it costs when you query the column. This column can calculate other column into the values and decltypes you need for ODBC.
ALTER TABLE data ADD COLUMN
Datepart date AS (date(LastModifiedDate))
Then to get the Datepart, you simply query the column.
SELECT Datepart FROM data

Reading dates from Sqlite with Delphi / Firedac

When reading date fields from Sqlite into Firedac, I get conversion errors. The fields are called dates but with string entries (yyyy-mm-dd). I set the option for Datetime Format = string, but I've discovered that while null values are handled OK, empty values (= '') produce an error which I can't figure out how to handle.
You can enable StrsEmpty2Null option, which will automatically convert all empty strings to NULL state. But it's for all values and parameters handled by the data component. So it's not the cure.
I'm not sure what you're doing, but in general, NULL is a state and you cannot convert NULL state to a value because it's a state indicating no value. So as you cannot convert empty string to date.
So try to describe more about your value to string conversion, so we can suggest a proper way to deal with it. For SQLite, I'd suggest using DATE pseudo data type and convert values through the built-in formatting expressions.

How to know which in *format* is a date returned, in ASP.NET

The following code, d has the current date. Depending on the current locale, it will return a date.
Dim d As Date = Date.Today
Note: I don't want to check whether the date is valid or not, but rather to know, whether it is in a 'dd-MM-yyyy', 'MM-dd-yyyy' or any other date format..
EDIT (29/06/2012 - Friday):
The reason I am asking this question is because I am sick of trying to deal with dates in ASP.NET. I build a project on my local PC, where dates are "dd/MM/yyyy" and as soon as I upload it to the production server (usually in US, hence MM/dd/yyyy) the code breaks.
So I usually deal with dates by converting them into yyyyMMdd format and also keep them in the database like that. That is the closest I get to an exception-free coding.
In this case, it makes sense that there is no way to get the format from a returned date string. Therefore, I will carry on with my approach.
Date.Today is a DateTime, not a string. Thus, it does not have an inherent format.
It will return a Date, which I believe is a VB alias for DateTime. (If it's not, just use DateTime explicitly to be idiomatically .NET rather than using the legacy VB types.)
A DateTime value itself doesn't have a format, any more than an int is in decimal or hex. It's only when you convert the value to a string that a format is applied, and then it depends on how you convert it to a string. You shouldn't use a string conversion until you really need to, and then you should control the format so that it works the way you want it to.
As far as possible, convert text data into its "natural" type as early as possible, and keep it in that type for as long as possible. For example, avoid converting to strings when passing values in SQL queries - instead, use parameterized SQL where you can specify the parameter value as a DateTime.
What you want to know is found in:
CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern
This is the format string that will be used when you call
d.ToShortDateString()
You need to be TOLD; there's no other way. Take for example:
6/12/2012
It's valid in dd/mm/yyyy or mm/dd/yyyy format
Which one do you pick if you don't know the locale/format beforehand?
Date values don't have an intrinsic format. In other words, you can format the date to any string representation you need but the opposite conversion from a string back to a Date value requires that you know in which format you are receiving this date string.

Date Format Problem

i cant change date value to the format which i want.This is my code.When i execute this code,i see mydate as mm/DD/yyyy format.what do i miss?
Dim dateString As String = Date.Now.ToString("dd.MM.yyyy")
Dim myDate As Date = Date.ParseExact(dateString, "dd.MM.yyyy", Nothing)
You don't miss anything. myDate is a Date which internally stores the date as a numerical form (as an UInt64 to be more precise). It does not store the string representation of the date, and it has no format in itself. When it is displayed as a string (for instance using the ToString method), if no date format is specified, it will use the default format for the current culture, which in your case seems to be MM/dd/yyyy.
Update
I will try to make this a bit more clear.
What you are experiencing is actually a rather common misunderstanding. You really need to make a difference between a date and the string representation of a date. These are two different things.
The Date (which is acutally a DateTime with no time information, so I will refer to DateTime here) stores the date and time internally as a number. Currently, where I am, this number is 9857259848526375120. That is the current date and time, as represented inside a DateTime object.
While this value is very useful to the code, it makes little sense for us humans. We have complicated the matter a bit more by deciding to write dates in different ways in different countries. So, here in Sweden, todays date would be written as "2009-09-17", while in Turkey it would be "17.09.2009".
So, in order to make things easier for us, whenever a DateTime object is displayed as text (in the VS debugger, when printed on screen and so on), the numerical value is formatted into a string using some date format. This format will be different in different countries. If no format is specified when displaying the string, the default format for the current culture will be used. This is the case in the VS watch window for instance.
If you wish to get the string representation of the DateTime object formatted according to a specific culture, you can pass it to the ToString method as such:
myDate.ToString(CultureInfo.GetCultureInfo("tr-TR"))
If you, on the other hand, need to convert the date into some other, specific format, you can use a format string to achieve that:
Dim formattedDate1 As String = myDate.ToString("dd'/'MM'/'yyyy")
Dim formattedDate2 As String = myDate.ToString("dd.MM.yyyy")
Note the single quotes around the / characters in the format string; they are needed since the system will by default interpret the / as a placeholder for the current culture's date separator. If you wish to force the formatted date to use a / character rather than the normal separator for the current culture ('-' in Sweden, '.' in Turkey), you need to escape it as in my example.

Resources