質問

「Timeago」を計算しようとしていますが、コードの説明を考えています。

今日の日付を使用しています

2010年11月7日

使用する場合 2010年9月1日 次に、私の.NETコードと私のJSコードの両方が「2か月」と言います

使用する場合 2010年8月31日 次に、私の.NETコードは「3か月」と書かれ、JSコードは「2か月」と表示されます

この不一致はまでとどまります 2010年8月9日.

基本的に、出会い系は、11月7日の今日の日付に基づいて、8月10日から8月31日まで「オフ」です。

これがJavaScriptです(から取られた」タイミアゴ「プラグイン」

    var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) ||
    seconds < 90 && substitute($l.minute, 1) ||
    minutes < 45 && substitute($l.minutes, Math.round(minutes)) ||
    minutes < 90 && substitute($l.hour, 1) ||
    hours < 24 && substitute($l.hours, Math.round(hours)) ||
    hours < 48 && substitute($l.day, 1) ||
    days < 30 && substitute($l.days, Math.floor(days)) ||
    days < 60 && substitute($l.month, 1) ||
    days < 365 && substitute($l.months, Math.floor(days / 30)) ||
    years < 2 && substitute($l.year, 1) ||
    substitute($l.years, Math.floor(years));

そして、これが私の.NETコードです(私によって書かれています)

    Public Function ToDuration(ByVal dt As Date?, _
                               Optional ByVal suffixAgo As String = Nothing) As String

        If Not dt Is Nothing Then
            Dim theDate As Date = dt
            Dim SecondsAppart As Integer = DateDiff(DateInterval.Second, theDate, Now)
            Dim output As String
            If SecondsAppart < 86400 Then
                Select Case SecondsAppart
                    Case Is <= 59 : output = "less than a minute " & suffixAgo
                    Case Is <= 119 : output = "about a minute " & suffixAgo
                    Case Is <= 3599 : output = DateDiff(DateInterval.Minute, theDate, Now) & " minutes " & suffixAgo
                    Case Is <= 7199 : output = "about an hour " & suffixAgo
                    Case Else : output = DateDiff(DateInterval.Hour, theDate, Now) & " hours " & suffixAgo
                End Select

            Else
                Dim DaysAppart As Integer = DateDiff(DateInterval.Day, theDate, Now)
                Select Case DaysAppart
                    Case Is <= 1 : output = "yesterday"
                    Case Is <= 30 : output = DateDiff(DateInterval.Day, theDate, Now) & " days " & suffixAgo
                    Case Is <= 60 : output = "about a month " & suffixAgo
                    Case Is <= 365 : output = DateDiff(DateInterval.Month, theDate, Now) & " months " & suffixAgo
                    Case Is <= 730 : output = "about a year " & suffixAgo
                    Case Else : output = DateDiff(DateInterval.Year, theDate, Now) & " years " & suffixAgo
                End Select
            End If

            Return output
        Else
            Return String.Empty
        End If
    End Function

ですから、私が抱えている問題は、基本的なものとロジスティックのものです。

  1. Datediffに関しては、どのコードが「正しい」ですか? (IE:2か月と14日は2か月または3日と見なされますか?)
  2. それに応じて並べるための最良の方法は何ですか?
役に立ちましたか?

解決

いくつかの仮定をして、C#でそれを書かなければなりませんでしたが、このバージョンのコードは8月31日に2か月と8月9日に3か月を与えてくれます

        public static string ToDuration(DateTime dt, string suffixAgo)
        {
            string output;
            DateTime theDate;
            if (dt == null)
            {
                output = "now";
            }
            else
            {
                theDate = dt;
                TimeSpan DateInterval = DateTime.Now - theDate;
                int SecondsAppart = Convert.ToInt32(Math.Floor(DateInterval.TotalSeconds));
                if ((SecondsAppart < 86400))
                {

                    if (SecondsAppart < 59)
                        output = ("less than a minute " + suffixAgo);
                    else if (SecondsAppart < 119)
                        output = ("about a minute " + suffixAgo);
                    else if (SecondsAppart < 3599)
                        output = string.Format("{0} minutes {1}", Math.Floor(DateInterval.TotalMinutes), suffixAgo);
                    else if (SecondsAppart < 7199)
                        output = "about an hour " + suffixAgo;
                    else
                        output = string.Format("{0} hours {1}", Math.Floor(DateInterval.TotalHours), suffixAgo);
                }
                else
                {
                    int DaysAppart = Convert.ToInt32(DateInterval.TotalDays);
                    if (DaysAppart <= 1)
                        output = "yesterday";
                    else if (DaysAppart < 30)
                        output = string.Format("{0} days {1}", Math.Floor(DateInterval.TotalDays), suffixAgo);
                    else if (DaysAppart < 60)
                        output = "about a month " + suffixAgo;
                    else if (DaysAppart < 365)
                        output = string.Format("{0} months {1}", Math.Floor(DateInterval.TotalDays/30), suffixAgo);
                    else if (DaysAppart < 730)
                        output = ("about a year " + suffixAgo);
                    else
                        output = string.Format("{0} year {1}", Math.Floor(DateInterval.TotalDays/365), suffixAgo);
                }
            }
            return output;
        }

コードを更新しましたが、今期待している結果があると思います。これが役立つことを願っています。

乾杯、ワーグナー。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top