Solved

Help with displaying date depending on IF statements

Posted on 2013-12-05
1
298 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
1 Comment
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now