Konwersja daty

0

Cześć, Napisałem prostą funkcję która konwertuje czas na np 1 godzinę temu itd. Mam problem jesli chodzi o 7 dni
Gdy powinno wypisać 7 dni temu wypisuje mi pełną date nie wiem czemu :(

        private const uint SECOND = 1;
        private const uint MINUTE = 60 * SECOND;
        private const uint HOUR = 60 * MINUTE;
        private const uint DAY = 24 * HOUR;
        private const uint WEEK = 7 * DAY;

            if(delta < DAY)
            {
                if(ts.Hours == 1)
                {
                    return "godzinę temu";
                }
                else if(ts.Hours <= 4)
                {
                    return ts.Hours + " godziny temu";
                }
                else
                {
                    return ts.Hours + " godzin temu";
                }
            }

            if(delta <= WEEK)
            {
                if(ts.Days == 1)
                {
                    return "wczoraj";
                }
                else
                {
                    return ts.Days + " dni temu";
                }
            }
            var item = new DateTime(2019, 8, 23);
            var item1 = new DateTime(2019, 8, 25, 10, 54, 0);
            var item2 = new DateTime(2019, 8, 25, 14, 54, 0);
            var item4 = new DateTime(2019, 8, 24, 14, 54, 0);
            //Ten test nie działa
            var item3 = new DateTime(2019, 8, 18, 19, 0, 0);

            var result = ConvertDate.ConvertRelativeDate(item);
            var result1 = ConvertDate.ConvertRelativeDate(item1);
            var result2 = ConvertDate.ConvertRelativeDate(item2);
            var result4 = ConvertDate.ConvertRelativeDate(item4);

            //Nie działa
            var result3 = ConvertDate.ConvertRelativeDate(item3);

            Assert.AreEqual("2 dni temu", result);
            Assert.AreEqual("8 godzin temu", result1);
            Assert.AreEqual("4 godziny temu", result2);
            Assert.AreEqual("wczoraj", result4);
            //Nie działa
            Assert.AreEqual("7 dni temu", result3);
1

W jaki sposob obliczasz "delta", nie widac tej czesci kodu. Probowales debugowac aplikacje?

0
            var ts = new TimeSpan(DateTime.Now.Ticks - youtDate.Ticks);
            double delta = Math.Abs(ts.TotalSeconds);

Kurcze debuguje i niby działa tylko problem jest z tymi 7 dniami

0

Znaczy mam tak:

            if(delta <= WEEK)
            {
                if(ts.Days == 1)
                {
                    return "wczoraj";
                }
                else
                {
                    return ts.Days + " dni temu";
                }
            }

            return youtDate.ToString();

A całość wygląda tak:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Blog.Utils
{
    public class ConvertDate
    {
        private const uint SECOND = 1;
        private const uint MINUTE = 60 * SECOND;
        private const uint HOUR = 60 * MINUTE;
        private const uint DAY = 24 * HOUR;
        private const uint WEEK = 7 * DAY;

        public static string ConvertRelativeDate(DateTime youtDate)
        {
            var ts = new TimeSpan(DateTime.Now.Ticks - youtDate.Ticks);
            double delta = Math.Abs(ts.TotalSeconds);

            if(delta <= MINUTE)
            {
                if(ts.Seconds == 1)
                {
                    return "sekunde temu";
                }
                else if(ts.Seconds <=4)
                {
                    return ts.Seconds + " sekundy temu";
                }
                else
                {
                    return ts.Seconds + " sekund temu";
                }
            }
            if(delta <= HOUR)
            {
                if(ts.Minutes == 1)
                {
                    return "minute temu";
                }
                else if (ts.Minutes <= 4)
                {
                    return ts.Minutes + " minuty temu";
                }
                else
                {
                    return ts.Minutes + " minut temu";
                }
            }

            if(delta <= DAY)
            {
                if(ts.Hours == 1)
                {
                    return "godzinę temu";
                }
                else if(ts.Hours <= 4)
                {
                    return ts.Hours + " godziny temu";
                }
                else
                {
                    return ts.Hours + " godzin temu";
                }
            }

            if(delta <= WEEK)
            {
                if(ts.Days == 1)
                {
                    return "wczoraj";
                }
                else
                {
                    return ts.Days + " dni temu";
                }
            }

            if(delta > WEEK)
            {
                return youtDate.ToString();
            }

            return youtDate.ToString();
            
        }

    }
}
1

Nie rozumiem. Po co właściwie tworzysz WEEK skoro funkcja ma zwracac ilość dni? Gdy delta jest większa od week funkcja zwraca Ci po prostu stringa daty, a to nie jest to czego oczekujesz. Dopisz co ma wtedy robić. I chyba lepiej korzystać w takich przypadkach z else if niż z osobnych ifów.

0

Znaczy chciałem aby po 7 dniach Data była juz nie konwersowana na ten tekst
Chciałbym aby było że gdy jest tydzien 7 dni. Pisało 7 dni temu a kolejny dzień zamiast 8 dni temu to juz normalna data

1

Nie wiem czy dobrze ale nie lepiej spróbować coś takiego?

public string ConvertRelativeDate(DateTime Date)
{
	DateTime CurrentDate = DateTime.Now;
	if(Date.Ticks > CurrentDate.Ticks)
		throw new Exception("Podana data <Date> jest wyższa od aktualnej <DateTime.Now>");
	TimeSpan timeBetween = new Timespan(CurrenDate.Ticks - Date.Ticks);
	if(timeBetween.totalSeconds == 1)
		return "Sekundę temu";
	if(timeBetween.totalSeconds <= 4)
		return timeBetween.totalSeconds.ToString() + " sekundy temu";
	if(timeBetween.totalSeconds < 60)
		return timeBetween.totalSeconds.ToString() + " sekund temu";
	if(timeBetween.totalMinutes == 1)
		return "Minutę temu";
	if(timeBetween.totalMinutes <= 4)
		return timeBetween.totalMinutes.ToString() + " minuty temu";
	if(timeBetween.totalMinutes < 60)
		return timeBetween.totalMinutes.ToString() + " minut temu";
	if(timeBetween.totalHours == 1)
		return "Godzinę temu";
	if(timeBetween.totalHours <= 4)
		return timeBetween.totalHours.ToString() + " godziny temu";
	if(timeBetween.totalHours < 60)
		return timeBetween.totalHours.ToString() + " godzin temu";
		
}

i potem możesz analogicznie do kodu dodać "totalDays", "totalMonths", "totalYears"

1 użytkowników online, w tym zalogowanych: 0, gości: 1