Converting string to Date

Hi.

I am importing data from a text file, content looks as below:

date_t,category
2015-12-18,Result Presentation
2015-12-15,Result Presentation
2015-10-29,Board Meeting Result Disscussion
2015-09-29,Result Presentation
2015-09-11,Result Presentation
2015-09-09,Result Presentation
2015-09-04,Insider Trading
2015-09-04,Insider Trading
2015-08-17,Result Presentation

Here is the code I am using to convert the first column to date.

for(i=0; i < osTabGetRowCount(table3); i++)
{
osdate[i] = 	StrToDateTime (osTabGet( i, 0, table3)) ;
}

I want the osdate (the first column) array to be same format as datenum().

This is how the output compares with DateNUM().

my code is not working as expected.

Any help please?

DateTimeConvert()
https://www.amibroker.com/guide/afl/datetimeconvert.html

@fxshrat

This function expects the input to be in date format. Mine is text. I am still now clear how does this function helps for this purpose?

StrToDatetime returns type string? Are you serious?
How about taking a look into function reference guide once in a while…

@fxshrat

Says Who?

When did I say that?

How about reading the question properly?

Perhaps you are looking for this, StrToDateTime

It is in the User Guide, good luck

https://www.amibroker.com/guide/afl/strtodatetime.html

@Ankur, so you don’t want to convert to type DateNum all of the sudden?
Fine. But then you would have to change your topic.

@PortfolioBuilder, he is already using StrToDateTime in his first post. All he has to do is converting to DateNum via DateTimeConvert but apparently either I’m talking Martian or he is trying to argue with oldtimers.

Ah, I missed that ! Thanks.

@Ankur - the CODE WORKS FINE.

StrToDateTime() function converts from string to DateTime format correctly (see below)

What is wrong are your assumptions that it does not work.

If instead of assuming things you read the manual: http://www.amibroker.com/guide/afl/datetime.html you would see:

It is important to understand that DateTime is not a simple number but rather bitset

The mistake you are doing is assuming that datetime is a “normal” number. It is not. It is encoded value (opaque to human) because there is NOT enough space to encode date time WITH second resolution inside 32-bit number. So AmiBroker uses heavy magic here to make it possible and these are not normal numbers. They are packed bitfields that would produce weird values if displayed as numbers.
The ONLY way to display human-readable value from datetime field is to use DateTimeToStr(). This function knows how to convert datetime to human-readable.

A good advice is here: http://www.goodreads.com/quotes/192478-you-should-never-assume-you-know-what-happens-when-you - have it printed and remembered.

4 Likes

When I think that HFT traders have softwares that deal with nano-seconde,
something like
12 h 05 mn 38 s 589 ms 999 µs 047 ns

Do NOT hijack threads. Keep strictly on topic.
Microsec() and Millisec() functions ARE available that provide finer granularity (if your data vendor sends that - most don’t) but this is OFF-TOPIC with regards to this thread.
Thread is closed.