Question

Our db server is outside of the country.. So i stored the created date using TimeZoneInfo like following,

DateTime dateTime = DateTime.Now;
        var timeZone = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dateTime, TimeZoneInfo.Local.Id, "India Standard Time");

In page, i am showing used by Timespan. I create separate class for that..

My code

 public static string GetFriendlyDate(DateTime dateTime)
    {
        TimeSpan ts = DateTime.Now.Subtract(dateTime);

        string friendlyDate = dateTime.ToShortDateString();
        int totalDays = (int)System.Math.Round(ts.TotalDays);
        int totalHours = (int)System.Math.Round(ts.TotalHours);
        int totalMinutes = (int)System.Math.Round(ts.TotalMinutes);
        int totalSeconds = (int)System.Math.Round(ts.TotalSeconds);
        int totalMilliSeconds = (int)System.Math.Round(ts.TotalMilliseconds);

        int totalMonths = totalDays / 31;  //approx.. change this
        int totalYears = totalDays / 365; //approx.. change this

        if (totalYears > 0) //give in terms of years
        {
            if (totalYears == 1)
                friendlyDate = "last year";
            else
                friendlyDate = totalYears + " years ago";
        }
        else if (totalMonths > 1) //give in terms of months
        {
            if (totalMonths == 1)
                friendlyDate = "last month";
            else
                friendlyDate = totalMonths + " months ago";
        }
        else if (totalDays > 1) //give in terms of days (at least 2 days)
        {
            friendlyDate = totalDays + " days ago";
        }
        else if (totalHours > 0) //give in terms of hours
        {
            if (totalHours == 1)
                friendlyDate = "1 hour ago";
            else
                friendlyDate = totalHours + " hours ago";
        }
        else if (totalMinutes > 0) // give in terms of minutes
        {
            if (totalMinutes == 1)
                friendlyDate = "1 minute ago";
            else
                friendlyDate = totalMinutes + " minutes ago";
        }
        else if (totalSeconds > 0) //give in terms of seconds
        {
            if (totalSeconds == 1)
                friendlyDate = "1 second ago";
            else
                friendlyDate = totalSeconds + " seconds ago";
        }
        else //just now
        {
            friendlyDate = "a moment ago";
        }

        return friendlyDate;
    }

when i run local it shows correctly "--seconds ago"... like that.. But in server it is always showing a moment ago, after some hours it is taking "---hours ago" like that..

can anyone please help me out of this problem?

Était-ce utile?

La solution

If i want to get a correct way what i asked my question, i have to convert the time into UTC.

so i changed that..

var utcTime = DateTime.UtcNow.AddHours(5).AddMinutes(30);
        TimeSpan ts = utcTime.Subtract(dateTime);

Now the issues is solved...

Autres conseils

You were storing items as of India Standard Time, and then comparing them against the local time.

The solution you gave in your answer is adjusting the time back to India Standard Time, which is fixed at +5:30 offset from UTC. This only works because IST does not have any daylight saving time rules. If you were using a different time zone, such as US Eastern Time, it would not work reliably.

The correct solution is to store the original value as UTC. Instead of using DateTime.Now and converting to IST, just use DateTime.UtcNow and store the value directly. When you compare in your GetFriendlyDate method, you should also use DateTime.UtcNow as the basis for comparison.

If you already have data saved in your database in India Standard Time, then you will need to update those values when you make this change. For example, if this is SQL Server, you could run the following script to update your values from IST to UTC:

UPDATE mytable SET thedatetime = DATEADD(minute, -330, thedatetime)

In general, local date/time values such as those retrieved from DateTime.Now have no business being in a web application. Read The Case Against DateTime.Now.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top