Need date in "dd-mm-yyyy" format in JSON - json

I need to give date in "dd-mm-yyyy" format in REST API. But the API response always comes in "yyyy-mm-dd" format even if i changed the format of date field to "99-99-9999". It seems it always gives date in ISO 8601 format no matter what format i choose.
I checked session:date-format and it's already dmy. "write-json()" method also has the same problem. But i only need it in REST webservice. Progress verison: 11.3.
Please see this for more clarification:
DEFINE TEMP-TABLE ttdate
FIELD fdate AS DATE FORMAT "99-99-9999".
CREATE ttdate.
ASSIGN ttdate.fdate = TODAY.
CREATE ttdate.
ASSIGN ttdate.fdate = TODAY - 15.
TEMP-TABLE ttdate:WRITE-JSON("file", "D:/ttdate.json", YES).
{"ttdate": [
{
"fdate": "2019-02-19"
},
{
"fdate": "2019-02-04"
}
]}
In JSON, it always gives in YYYY-MM-DD no matter what format i choose. Please don't suggest to use string it will be a huge pain for me to use string.Please note that I am concerned about date format in JSONs only.

You can always keep it simple/force it as a character instead of a date:
DEFINE VARIABLE dt AS DATE NO-UNDO.
DEFINE VARIABLE c AS CHARACTER NO-UNDO FORMAT "x(12)".
dt = TODAY.
c = STRING(DAY(dt),"99") + "-" + STRING(MONTH(dt),"99") + "-" + STRING(YEAR(dt), "9999").
DISPLAY c .
31-01-2019
However, this really works for me, for this use case (displaying).
SESSION:DATE-FORMAT = "dmy".
DISP TODAY FORMAT "99-99-9999".

#Jensd first solution sounds like what you will need in your case. When using the WRITE-JSON method, I don't think you have any control over the format of the data. In cases where the other end needs very specific formats for data, a string is sometimes the only way to get it.

Related

Coldfusion datepicker dd/mm/yyyy but MySQL stay with format yyyy/mm/dd

Datepicker is an option for user easy to pick the date to fill the form. In coldfusion, there is a fill form that need to use datepicker and after user selected the date and fill the form with format yyyy/mm/dd by default format which is MySql can read. If i change into dd/mm/yyyy and click save into MySql will get error because from what i know default format for MySql is yyyy/mm/dd.
<input type="text" name="Date_joined" size="auto" style="border:0px"required="yes">
This is the function to popup datepicker :
<a href="javascript:showCal('Calendar1')">
This is logo for datepicker :
<img align="right" src="calendar_menu.gif" alt="Select a date" border="0"></a>
Is there any solution for user pick a date and input text will display dateformat dd/mm/yyyy but still can save into MySql without error.
How to make MySQL accept a user input in format DD/MM/YYYY so the data will be recorded
If you want to save a string of numbers and dashes that represents a date in the format you want it displayed on a screen, then just make your database column a CHAR(10) and be done with it.
But, if you want to do calculations against it, aggregate data by it, DO THINGS with it, then save it as a date type. Don't worry about how your database UI represents that date value to you. Maybe it's different from how you want it shown on an HTML page, it doesn't matter. What matters is that as a date object, you can easily use and display that value however you like.
From what I know, MySQL will only accept datatype date with format YYYY/MM/DD.
Not how that works.
https://dev.mysql.com/doc/refman/5.7/en/datetime.html
The DATE type is used for values with a date part but no time part.
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. > The supported range is '1000-01-01' to '9999-12-31'.
See that "retrieves and displays" (emphasis mine)? It's just a date object with "00:00:00" as the time portion.
So however your form field accepts the string representation of the date, you need to convert it to a proper date object. Per Dan's suggestion, you can easily use parseDateTime() to accomplish this.
#writeOutput( parseDateTime( now() ) )# will output {ts '2018-03-14 15:29:19'}.
If your form field contains a valid string that represents a date (e.g. 2018-03-14):
#writeOutput( parseDateTime( form.myDateField ) )# will output {ts '2018-03-14 00:00:00'}.
So the value will be saved as a date object, without the time portion. When you read the saved value later, just use dateFormat() to display it in any format you like.
(Moved from comments for greater visibility)
One important addition to Adrian's answer. In this specific case, you MUST use a date mask with parseDateTime(). The mask controls how parseDateTime() interprets the input. Without the correct mask, the results may be wrong for your specific input, namely dd/mm/yyyy.
TryCF Example
Code:
<cfscript>
dateString = "05/08/2018";
writeOutput("<br>Without mask = "& parseDateTime( dateString) );
writeOutput("<br>With mask = "& parseDateTime( dateString, "dd/MM/yyyy") );
</cfscript>
Result:
Without mask = {ts '2018-05-08 00:00:00'} (May 8, 2018)
With mask = {ts '2018-08-05 00:00:00'} (August 5, 2018)

How to check if a string contains a date?

I'm trying to iterate on a DataSet, this contain a results of query such as SELECT * FROM tb 1, now the first three field contains a date, the format saved in the database table is this:
yyyy-MM-dd HH:mm:ss
but the code return this:
yyyy/MM/dd HH:mm:ss
in particular this:
For z = 1 To ds.Tables(0).Columns.Count - 1
Console.WriteLine(ds.Tables(0).Rows(x)(z).ToString())
Next
So I need to recognize if the current string have this format: yyyy/MM/dd HH:mm:ss and parse it into: yyyy-MM-dd HH:mm:ss I tough to a regex pattern for recognize it, but I'm not an expert of regex. Anyway, if there is another solution I'll glad to see. Note that only the first three value and the last one of the table is date, the other values aren't date but contain integer or other string value.
Dates do not have a format. From MSDN:
Represents an instant in time, typically expressed as a date and time of day.
...
Time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.) in the GregorianCalendar calendar...For example, a ticks value of 31241376000000000L represents the date, Friday, January 01, 0100 12:00:00 midnight.
So, a DateTime is just a Big Number. Representing them as "dd/MM/yyyy" is part of the magic of the DateTime type. Part of the issue is this:
Console.WriteLine(ds.Tables(0).Rows(x)(z).ToString())
Row items are Object. It wont act like a DateTime type unless/until you get it into a DateTime variable. That print as a DateTime simple because the DataTable knows the underlying type; but it will use the default format for your Culture. This makes it look like dates have a built in format (or even that the "format changed" if you tried to set it to something), but you are a human and 635882810022222112L would not make sense to most of us.
To change the output style, you first need to get it into a DateTime variable. Apparently, a preliminary step is to determine if an arbitrary column is a Date. Rather than testing the "format" of the output, test the underlying data type. This does assume a proper DateTime column in the DataTable:
If ds.Tables(0).Columns(n).DataType = GetType(DateTime) Then
'...
End If
' Or:
If ds.Tables(0).Rows(x)(z).GetType Is GetType(DateTime) Then
'...
End If
Then to change the display, first get it into a DateTime variable:
Dim dt As DateTime
If ds.Tables(0).Rows(x)(z).GetType Is GetType(DateTime) Then
dt = Convert.ToDateTime(ds.Tables(0).Rows(x)(z))
' cant change "format" but you can change how it displays:
Console.WriteLine(dt.ToLongDateString)
Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm tt"))
Console.WriteLine(dt.ToString("dd MMM, yyyy"))
End If
An easier way to get and convert to DateTime is to use the Field(Of T) extension:
Dim dt = ds.Tables(0).Rows(x).Field(Of DateTime)(y)
when I peform the insert usually do this: Date.Now.ToString("yyyy-MM-dd HH:mm:ss") so I apply a format to date to insert... if I don't format correctly the date as I shown I get this value 0000-00-00 00:00:00
That doesn't apply a format to a date. It converts the DateTime to a string. While "yyyy-MM-dd HH:mm:ss" is the correct format to use when passing date data as a string to MySql, it is not needed. The MySQL Data provider knows how to convert a Net DateTime var to the data MySql needs/wants and back again -- that's its job.
' this will work fine
cmd.Parameters.Add("#SomeDate", MySqlDbType.DateTime).Value = myDateTimeVar
The format requirement you read about is the what you need to use in the MySql shell or WorkBench UI because you are entering text/string there...from the keyboard. It does not mean code must convert DateTime variables to string in a specific format for storing.
I ended up using this
Try
Dim theDate As DateTime = dr.Item(colName)
Return theDate
Catch
' do something
End Try
I would be happy to see a better method.
Based off of what you seem to be asking a simple replace would do
For z = 1 To ds.Tables(0).Columns.Count - 1
Console.WriteLine(ds.Tables(0).Rows(x)(z).ToString().Replace("/","-"))
Next
if it comes in with / they are changed to - if it comes in with - they remain intact.
Depending on the flexibility you want in this, it may be necessary to TryParse to ensure that the value you're working with is actually a valid datetime.

dd/mm/yyyy date format in SSRS

i'm trying to specify dd/mm/yyyy dateformat for date/time parameter in SSRS 2008 R2.
My computers datetime format is mm-dd-yyyy.
My requirement is, i want to show date format at dd/mm/yyyy irrespective of the system/server date format.
I've already tried CDate(Format(Today,"dd/mm/yyyy")) which didn't work. one very strange thing i observed is, it shows dd/mm/yyyy format only for dates on or before 12-MM-yyyy, and 13 onwards it gives error: Conversion from string '25-04-2014' to type Date is not valid. (Possibly it is trying to map 25(daypart) with MM-dd-yyyy (month part)) which is out of range of total months i.e. 12)
my research on internet says it is a bug in BIDS 2008.
What do i do to display date as dd/mm/yyyy ??
I don't have enough reputation to comment, but I did notice that you failed to put "()" after "Today". If I'm not mistaken you must put Today() for that function to work. Also, you might want to try putting CDate Around the Today() function. You shouldn't need it, but it's worth a shot. Also, for some odd reason, in my experience, you must capitalize MM for format to work correctly.
Like #Aditaya said, it should be =format(Today(),"dd/MM/yyyy")
The expression I usually use is:
=FormatDateTime(Fields!Date.Value, DateFormat.ShortDate)
However, this may be region specific.
Rather than writing an expression to do the formatting, you can also use the Textbox Format Property. But first you need to make sure that the data is in a date format. So use the CDate function on your column like this:
=CDate(Fields!Date.Value)
Then in the textbox properties go to the Number tab. Select Date for the category. Then you can select whichever format you want or use a Custom format. This will change how the column displays when you run the report.

reverse date formatting in as3

As3 has some functions that help with converting a date to various formats of strings, but I cannot find any functions that do the opposite.
in the case of
2012-04-16 I'd like to use YYYY-MM-DD as my format string to get a date object
01/01/2013 12:00.13 I would specify `DD/MM/YYYY HH:NN.SS
Is ther something that does the reverse/opposite of formatDate?
Leveraging off a similar question (Parse string pattern into Date in Flex), you can use DateField.stringToDate to get the date portion of the string:
var theDate:Date
theDate = DateField.stringToDate('2012-04-16', 'YYYY-MM-DD'); // Returns 16 April 2012
theDate = DateField.stringToDate('01/02/2013', 'DD/MM/YYYY'); // Returns 1 Feb 2013
Unfortunately this only parses the date, not the time.
There are examples of using RegExp to parse out the different date-time components and then creating the Date with the values thus parsed, but I've not seen or done anything like the generic format string approach.

How can I get the date format from an SSRS report from the web service interface?

I get report params in C# code from the webservice as follows:
ReportingService2005 ReportingService = ConnectToSSRS();
ReportParameter[] ReportParams = ReportingService.GetReportParameters(reportSelector.SelectedValue, HistoryID, ForRendering, emptyParams, null);
DateTime parameters currently seem to always come up as mm/dd/yyyy. Where is the date format defined? It's not hardcoded like this is it? Is there a way for me to obtain the DateFormat from code?
Thanks
What you're seeing is the default format string representation of the DateTime object (according to your locale...probably), so yes, in a sense, it is harcoded like that.
If you're working with a DateTime object though, rather than try and figure out what format it is in, you could just force it to whatever format you like best.
dt.ToString("MM/dd/yyyy hh:mm:sszzz")
You might want to take a look here for information that might help you.
Also this.
Try to use ISO 8601 date format, e.g. 1999-06-01. It will work.