Date formatting in Lua - date

I am retrieving a date from a database in the following format:
vardate = '01/20/2017 09:20:35' - mm/dd/yyyy hh:mm:ss
I want to convert it to the format dd-mm-yyyy hh:mm:ss
Can I get some guidance on how I could get the format I want?

Date formatting in Lua is pretty simplistic. If you only need to convert it from one format to another, and that format does not change, you could simply use string.match:
function convertDate(vardate)
local d,m,y,h,i,s = string.match(vardate, '(%d+)/(%d+)/(%d+) (%d+):(%d+):(%d+)')
return string.format('%s/%s/%s %s:%s:%s', y,m,d,h,i,s)
end
-- Call it this way
convertDate('01/20/2017 09:20:35')
If you need something more involved, I suggest using an external library.

function ConvertDate(date)
return (date:gsub('(%d+)/(%d+)/(%d+) (%d+:%d+:%d+)','%2-%1-%3 %4'))
end
-- test
print(ConvertDate('01/20/2017 09:20:35'))

Related

Matlab - isbusday and busdate format change

Would you be able to advise how can i change the format of the date the functions isbusday and busdate are using ?
The functions use US date format by default, but I need them to be in European format dd/mm/yyyy.
I have attempted to use the code below but it is not working.
isbusday('01-01-2015','dd-mm-yyyy')
busdate('01-01-2015','dd-mm-yyyy',1)
Thank you very much.
You want to convert your string to a datetime object. That is where you can control the format:
d = datetime('01-01-2015','InputFormat','dd-mm-yyyy');
isbusday(d)
busdate(t)
See the documentation: https://www.mathworks.com/help/matlab/ref/datetime.html and https://www.mathworks.com/help/finance/isbusday.html

using DateFormat to produce a "2016-12-28T17:43:47.345Z"

I have been looking at the above question and have most of it correct.
I am going to get a datetime in Zulu, and then will want to output that format.
My first go is just as simple as:
DateFormat format = new DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
My issue I am having is the T and Z. Obviously T is used to separate the date from the time and the Z is representative of Zulu time.
That being said the users will be entering a datetime in Zulu, so it wont need to be converted from Local to Zulu, so i was not sure if 'Z' is an acceptable result. I was not sure if there is a different want to handle this, or if my result was the best answer.
Try this package, Jiffy.
String isoFomart = Jiffy().format(); // This will return ISO format from now
You can also add your DateTime object
String isoFomart = Jiffy(DateTime.now()).format(); // This will also return ISO format from now
Hope this helped
The DateTime object has a method called: toIso8601String which is used to return an ISO formatted string. The 'Z' will be added if isUTC is true, otherwise the result will not have the Z in it.
Make sure that the DateTime object itself is correctly set to UTC as if you look in the constructor for the class will tell you a lot of the defaults are local with the exception of the DateTime.utc() static function.
In that concept, you dont really need a DateFormat use to define an iso string.

Format date and add month to it

I'm currently working with embarcadero c++, this is the first time I'm working with it so it's completely new to me.
What I'm trying to achieve is to get the current date, make sure the date has the "dd/MM/yyyy" format. When I'm sure this is the case I want to add a month to the current date.
So let's say the current date is 08/18/2016 this has to be changed to 18/08/2016 and then the end result should be 18/09/2016.
I've found that there is a method for this in embarcardero however I'm not sure how to use this.
currently I've only been able to get the current date like this.
TDateTime currentDate = Date();
I hope someone will be able to help me out here.
I figured it out.
After I've searched some more I found the way to use the IncMonth method on this page.
The example given my problem is as follows:
void __fastcall TForm1::edtMonthsExit(TObject *Sender)
{
TDateTime StartDate = edtStartDate->Text;
int Months = edtMonths->Text.ToInt();
TDateTime NextPeriod = IncMonth(StartDate, Months);
edtNextPeriod->Text = NextPeriod;
}
After looking at I changed my code accordingly to this
TDateTime CurrentDate = Date();
TDateTime EndDate = IncMonth(CurrentDate, 1);
A date object doesn't have a format like "dd/MM/yyyy". A date object is internally simply represented as a number (or possibly some other form of representation that really isn't your problem or responsibility).
So you don't have to check if it's in this format because no date objects will ever be in this format, they simply don't have a format.
You will have to do additions/subtractions on the Date object that the language or library gives you, THEN (optionally) you can format it to a human-readable string so it looks like 18/08/2016 or 18th of August 2016 or whatever other readable format that you choose.
It might be that the TRANSFER of a date between 2 systems is in a similar format, but then formatting the date like that is entirely up to you.
As for how to do that, the link you posted seems like a possible way (or alternatively http://docwiki.embarcadero.com/Libraries/Berlin/en/System.SysUtils.IncMonth), I'm afraid I can't give you an example as I'm not familiar with the tool/language involved, I'm just speaking generically about Date manipulations and they should ALWAYS be on the raw object.

Date format in Talend "2006-05-27 17:00:00.000"

I am facing an issue with Talend dates. I have tried several solutions but still an "unparseable date" error persists.
My date format is of the form : "2006-05-27 17:00:00.000"
Can you help me ?
you can use below talendDate function to parse your string into date..
TalendDate.parseDate("yyyy-MM-dd HH:mm:ss.sss","2006-05-27 17:00:00.000")
this would take input as string and return you date.
If you don't handle the conversion yourself in a tMap but just want to use a schema, then: In your mapping configuration in the date field, you can add the following string:
yyyy-MM-dd HH:mm:ss.SSS
to set the correct format mapping for the date string. Otherwise the answer of garpitmzn is the way to go.
you should use this function in talend to fetch date:
TalendDate.parseDate("yyyy-MM-dd HH:mm:ss.SSS","2016-01-12 12:45:00.000")

Grails: how to parse date.toString() without making a custom formatter?

I have a Grails application that needs to parse Dates out of strings that were created with the date.toString() method.
My system's default date.toString() format is "Thu Apr 20 00:27:00 CEST 2006" so I know I can turn a Date into a string and then back into an object using Date.parse('EEE MMM dd HH:mm:ss z yyyy', new Date().toString()).
But that's lame! I shouldn't have to reverse engineer the system's default date format. Plus, I'm not sure under what circumstances the default date format can change, thus breaking that code.
Is there a way to parse a date.toString() back into a Date without using a hand-rolled formatter like that?
Thanks!
Update: I filed this Jira ticket to get such a feature added Groovy. Someone commented on the ticket that Java's date.toString() method is hard-coded to use EEE MMM dd HH:mm:ss z yyyy. That sucks for Java to be so inflexible, but it makes it easier for me to live with hard-coding the formatter!
There's a page over here showing how bad this is in Java (and hence in Groovy)
I agree that there should be a Date.parse method in Groovy which uses this default Date.toString() format.
Maybe it's worth adding a request for improvement over on the Groovy JIRA?
As a temporary workaround, you could add your parse method to the String metaClass in Bootstrap.groovy?
String.metaClass.parseToStringDate = { Date.parse( 'EEE MMM dd HH:mm:ss z yyyy', delegate ) }
Then you can do:
new Date().toString().parseToStringDate()
anywhere in the groovy portions of your grails app
I haven't worked with Grails and I know this is not the answer to your question, but as a workaround, couldn't you just save the format-string as a global variable?
if u use it to convert to json, maybe code below could help:
//bootstrap
JSON.registerObjectMarshaller(Date) {
return it?.format("yyyy-MM-dd HH:mm:ss")
}