lemunk
lemunk

Reputation: 2636

Convert datetime format wont format correctly

This one makes me feel like an idiot but i cannot figure out why this isnt working.

I have String

"6/9/2014 9:30:20 AM"

I parse this string to DateTime.

DateTime myTempDate = DateTime.Parse(date);

It now reads the same, ok now i want to shorten it to just a month.

myTempDate.ToString("MMMM");

But the result remains the same...

I have tried Convert.ToDatetime i have also tried other formats, it never changes.

What is going on here??

UPDATE:

ok seems the methodolgy is correct and im not going insane, so something else must be going on here...

Some extra info if this helps. This is all done in Umbraco 7.1.x

This is happening in a partial view.

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@using System.Web.Mvc.Html;
@using Umbraco.Web;
@using System.Globalization;
@{
// Get this blogs root, does not use an id because there may be more thanone blog
IPublishedContent blogRoot = Model.Content.AncestorOrSelf("SmartBlogBlog");
// Get the posts in this blog
IEnumerable<IPublishedContent> posts = blogRoot.Descendants("SmartBlogPost").OrderBy("updateDate");
// Create the tag dictionary
Dictionary<string, int> categoryList = new Dictionary<string, int>();
Dictionary<string, int> dateList = new Dictionary<string, int>();

// Loop through all the posts then loop through their tags to add to the tag dictionary
foreach (IPublishedContent post in posts)
{
    string[] categories = post.GetProperty("smartBlogCategory") != null
        && post.GetProperty("smartBlogCategory").Value != null
        && !string.IsNullOrEmpty(post.GetProperty("smartBlogCategory").Value.ToString())
        ? post.GetProperty("smartBlogCategory").Value.ToString().Split(',') 
        : new string[0];

    string[] dates = post.GetProperty("smartBlogDate") != null
        && post.GetProperty("smartBlogDate").Value != null
        && !string.IsNullOrEmpty(post.GetProperty("smartBlogDate").Value.ToString())
        ? post.GetProperty("smartBlogDate").ToString().Split(',')
        : new string[0];

    foreach (string category in post.GetProperty("smartBlogCategory").Value.ToString().Split(','))
    {
        if (categoryList.ContainsKey(category))
        {
            categoryList[category]++;
        }
        else
        {
            categoryList.Add(category, 1);
        }
    }

    foreach(string date in post.GetProperty("smartBlogDate").Value.ToString().Split(','))
    {
        if(dateList.ContainsKey(date))
        {
            DateTime tempDate = DateTime.Parse(date);
            tempDate.ToString("MMMM");
            string myTempDate = tempDate.ToString();
            dateList[myTempDate]++;
        }
        else
        {               
            DateTime myTempDate = DateTime.Parse(date);
            myTempDate.ToString("MMMM");
            string finalTempDate = myTempDate.ToString();

            dateList.Add(finalTempDate, 1);
        }
    }
}

<span class="smartSubTitle smartTopBorder">Categories</span><br />

// Loop through the tag dictionary
<ul>
    @foreach(KeyValuePair<string, int> category in categoryList)
    {
        //Deal with the tag
        <li>
            <span><a class="smartCategory" href="@Umbraco.NiceUrl(blogRoot.Id)[email protected]">@category.Key</a></span>
            <p>...........................</p>              
        </li>
    }
    @foreach(KeyValuePair<string, int> date in dateList)
    {
        <li>
            <span><a class="smartDate" href="@Umbraco.NiceUrl(blogRoot.Id)[email protected]">@date.Key</a></span>
            <p>...........................</p>              
        </li>
    }

</ul>
}

Upvotes: 0

Views: 271

Answers (3)

Arijit Mukherjee
Arijit Mukherjee

Reputation: 3875

TRY

string monthName = myTempDate.ToString("MMMM", CultureInfo.InvariantCulture);

Also

Can replace

DateTime myTempDate = DateTime.Parse(date);

with

DateTime myTempDate = DateTime.ParseExact(date, "d/M/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);

As suggested/Mentioned by : Sudhakar Tillapudi

Upvotes: 0

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

Try This:

string dt = "6/9/2014 9:30:20 AM";
DateTime dtFinal = DateTime.ParseExact(dt, "d/M/yyyy h:mm:ss tt",
                                                  CultureInfo.InvariantCulture);
String MonthName = dtFinal.ToString("MMMM");

EDIT:

You are not assigning the return value of the ToString() function in the below statement:

tempDate.ToString("MMMM");

You need to assign the return value of the ToString() function to some relavent variable as below:

String MonthName = tempDate.ToString("MMMM");

Upvotes: 1

cvraman
cvraman

Reputation: 1697

This is the problem. You are not using the return value. ToString does not modiy the existing value. It returns a new string. So instead of the code give below

tempDate.ToString("MMMM");
string myTempDate = tempDate.ToString();

myTempDate.ToString("MMMM");
string finalTempDate = myTempDate.ToString();

Change it to :

string myTempDate = tempDate.ToString("MMMM");
string finalTempDate = myTempDate.ToString("MMMM");

Upvotes: 1

Related Questions