Click here to Login

                                                   How to get the last day of the month in C#


2014-01-30 09:26:44

How do you add the last day of the month to a date field of a csv which only includes Year and Month?

I'm trying to modify the C# Pre Script in the Morningstar downloader. Currently the Morningstar downloader just adds a 1 for the day of month, possibly because that was simpler and good enough for the one who wrote it. I am trying to modify it to more accurately default to the last day of the month. Here is what I tried:
if (System.DateTime.TryParseExact(Content.Rows[2].Data[i],"yyyy-MM", CultureInfo.InvariantCulture, DateTimeStyles.None, dtDate)) { // The Morningstar csv date fields are formatted as yyyy-MM without the day of month
// Code to add DateTime.DaysInMonth(...)-1 and convert it back to a string goes here

But the compiler gave the error messages: The name 'CultureInfo' does not exist in the current context. The name 'DateTimeStyles' does not exist in the current context. Apparently there must be some way of adding a reference to them. How do I get this to work?


2014-01-30 20:02:51


Best Answer
Add (using System.Globalization;) at the beginning of the script.
Also, you must use the keyword "out" before "dtDate".

if (System.DateTime.TryParseExact(Content.Rows[2].Data[i],"yyyy-MM", CultureInfo.InvariantCulture, DateTimeStyles.None, out dtDate)

2014-01-31 04:04:00


That worked great thank you!

No more messages


No html code. URLs turn into links automatically.

Type in the trading objects you want to include: - Add Objects
To add a trading object in your message, type in the object name, select it and then click on "Add Objects"


Trading Items
Number of bars since the beginning of the month
Number of bars up to the end of the month
How far are the close prices from the support line
Trading Indicator: True between the first and last signal
Time-series Sum for the first Minutes/Hours of a Trading Day

How-to Lessons
How to plot the number of stock tweets per day
How to quickly select stocks based on the last value of a databas...
How to get stocks for a particular index using the global script
How to display the number of stocks per day for different RSI gro...
How to automate the data downloading process

Related Forum Threads
How to determine if the a given day is a trading day?
Replacing value for the last 100 bars (end of day data)
How to get the median closing price?
How do I get the date of the current bar in a custom function?
How do I find the close price for an entry matching a custom DB

Blog Posts
How to Display the Year-to-Date Excess Return in a Pivot Table
Sentiment Analysis: How to measure the sentiment score of your st...
How to predict and trade the stock market using pivot points
Buy the best/top rated stocks or how to create powerful rank base...
How to Pick the Best Trend Indicators

Create an account
Affiliate Program
Contact Us
Trading Forum
How-to Lessons
About Us
Terms of Use

Copyright 2021
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Follow us on Google+
RSS Trading Items

Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.