Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with displaying date depending on IF statements

Posted on 2013-12-05
1
Medium Priority
?
314 Views
Last Modified: 2013-12-06
Hi there

I hope someone can help me, I have a date and I need to do the following:

if date:

1. Drop the year if it is the current year
2. Drop the date if it is 'today'
3. Drop the time if it is the previous  year

thanks
0
Comment
Question by:mousemat24
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 2000 total points
ID: 39699404
You need different format strings. Please look at the code snippet. Code is for win forms but the idea is same for ASP. I have three buttons with different dates, see comments. Hope it will be clear.

        private string datreFormatStr1 = "{0:HH:mm}";
        private string datreFormatStr2 = "{0:dd MMMM HH:mm}";
        private string datreFormatStr3 = "{0:dd MMMM yyyy}";

        private void evaluteFormatAndDisplayDate(DateTime dt)
        {
            //1. Drop the year if it is the current year
            //2. Drop the date if it is 'today'
            //3. Drop the time if it is the previous  year
            string dateStr;
            int currentYear = DateTime.Now.Year;
            int currentMonth = DateTime.Now.Month;
            int currentDay = DateTime.Now.Day;

            if (dt.Year == currentYear)
            {
                if (dt.Month == currentMonth && dt.Day == currentDay)
                {
                    dateStr = string.Format(datreFormatStr1, dt);
                }
                else
                {
                    dateStr = string.Format(datreFormatStr2, dt);
                }

            }
            else
            {
                // Case3. Show year, month, day but not time
                dateStr = string.Format(datreFormatStr3, dt);
            }
            MessageBox.Show(dateStr);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //previous year
            DateTime dt = new DateTime(2012, 11, 27, 17, 55, 33);
            evaluteFormatAndDisplayDate(dt);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //this year but not today
            DateTime dt = new DateTime(DateTime.Now.Year, 10, 15, 13, 01, 27);
            evaluteFormatAndDisplayDate(dt);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            //today
            DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 16, 22, 03);
            evaluteFormatAndDisplayDate(dt);
        }

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question