reverse date formatting in as3 - actionscript-3

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.

Related

Convert date format while using date_add function in SQL

I have a variable (order_date_key) that contains the number of days since 1/1/1900. For example, 42711 represents December 9, 2016 or 42711 days since 1/1/1900. I want to convert that variable to the week of the year (e.g., 2016-50)
I was able to convert the variable to yyyy-mm-dd format using the date_add function, but when I try to use the DATE_FORMAT function with it, it returns an incorrect week number. For example, 2016-12-09 converted to 2016-5, but that date isn't the 5th week of the year.
Here's the code I'm using. The format also doesn't seem to work when I use the '%' symbol.
SELECT order_date_key,
DATE_ADD('1900-01-01', order_date_key) AS Year_month_day,
DATE_FORMAT(DATE_ADD('1900-01-01', order_date_key), 'Y-u') as year_week
Sample data: 42711, 42714, 42715, 42720
Desired output: 2016-50, 2016-51, 2016-51, 2016-52

How can I format the date from mysql (I'm using node.js)?

I'm using node.js to select info from my mysql database and I have a datetime column, but I'm not getting the format I want.
This is my sql code
SELECT id, data, titulo, subtitulo, texto, DATE(datahora_cadastro) as data FROM sig_noticias
I need the data to be like this: DD/MM/YYYY
But I'm getting this: Thu Mar 30 2017 00:00:00 GMT-0300 (GMT-03:00)
Easiest way to format dates in Javascript is by far using a modern date / time library. I personally use MomentJS and it works well. I highly recommend this solution if you plan on working with dates in even just a few areas of your app.
Using moment, you could simply call moment(myDate).format("DD/MM/YYYY");.
If you really want to stick to pure Javascript, however, here's how to get the format you want:
const formattedDate = `${myDate.getDate() + 1}/${myDate.getMonth() + 1}/${myDate.getFullYear()}`;
Javascript's built-in date functions are pretty confusing. Here, for instance, the getDate and getMonth methods return the index of the date / month, and not the actual date / month, so we need to add +1 to those.
Using vanilla Javascript then becomes even harder when dealing with date manipulation (adding, subtracting) and timezones.

Need date in "dd-mm-yyyy" format in 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.

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.

Representing Partial Dates in a JSON API

I am managing a RESTful JSON data API, which is growing in scope. Currently, we are returning all dates over the wire as strings "YYYY-MM-DD", but we also need to represent the concept of partial dates.
What I mean by a partial date is a date value that has unknown components, either a Year-Month, or just a Year. In our presentation layer, this would be translated like:
2009-09-03 => '3rd September 2009'
2009-09 => 'September 2009'
2009 => 'Undated 2009'
Is there any precedent or standard for this type of data? For example, MySQL enables this by allowing 00 values in date and datetime fields - eg: '2009-00-00' will save directly to a MySQL date field, but this value does not translate consistently when parsed by most programming languages' date libraries.
What is the best way to semantically represent this concept of partial dates in the JSON feed?
Ideally, we would be able to implement a solution that was easy for our consumers to parse, but also straightforward to explain in documentation.
EDIT: Thinking about this again, YYYY-MM and YYYY are both valid ISO 8601, so it would probably be better to just use that.
There is no standard for JSON dates (of any kind) because JSON is a subset of JavaScript literal syntax, and JS has no date literals. See http://msdn.microsoft.com/en-us/library/bb299886.aspx#intro_to_json_sidebarb .
In terms of storage, you can store the ambiguity by storing year, month, and day in separate columns, and allowing nulls. That way you can query it with string concatenation and the coalesce(day,'00') function to grab out a '00' if a column is blank.
You may find your solution # www.datejs.com
Thanks.