如何在网站上显示日期 [英] how to display date in website

查看:108
本文介绍了如何在网站上显示日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人告诉我如何在网站上显示日期格式为2012年7月27日
如何从数据库中获取日期

Pls some one tell that how to display date in website the format is 27th july 2012
how to take date from database

推荐答案


试试这个:
Hi,
Try this:
//String should be date and in dd MMM yyyy format
public static string GetOrdinal(string s)
{
    int Number = Convert.ToInt32(s.Split(' ')[0]);
    string suffix = String.Empty;
    if (Number.ToString().Length > 2)
    {
        int intEndNum = Convert.ToInt32(Number.ToString().Substring(Number.ToString().Length - 2, 2));
        if (intEndNum >= 11 && intEndNum <= 13)
            switch (intEndNum)
            {
                case 11:
                case 12:
                case 13:
                    suffix = "th";
                    break;
            }
    }


    if (Number >= 21)
    {
        //Handles 21st, 22nd, 23rd, et al
        int Number21 = Convert.ToInt32(Number.ToString().Substring(Number.ToString().Length - 1, 1));
        switch (Number21)
        {
            case 1:
                suffix = "st";
                break;
            case 2:
                suffix = "nd";
                break;
            case 3:
                suffix = "rd";
                break;
            case 0:
                suffix = "th";
                break;
            default:
                for (int i = 4; i <= 9; i++)
                {
                    if (Number21 == i)
                    {
                        suffix = "th";
                        break;
                    }
                    else
                        suffix = String.Empty;
                }
                break;
        }
    }
    else
    {
        switch (Number)
        {
            case 1:
                suffix = "st";
                break;
            case 2:
                suffix = "nd";
                break;
            case 3:
                suffix = "rd";
                break;
            default:
                for (int i = 4; i <= 21; i++)
                {
                    if (Number == i)
                    {
                        suffix = "th";
                        break;
                    }
                    else
                    {
                        suffix = String.Empty;
                    }
                }
                break;
        }
    }
    string NewDate = s.Split(' ')[0]+""+suffix + " " + s.Split(' ')[1] + " " + s.Split(' ')[2];
    return NewDate;
}


从任何地方调用上述函数,如:


Call the above function from anywhere like:

static void Main()
{
    string TodayDate = DateTime.Now.ToString("dd MMM yyyy");
    string date = GetOrdinal(TodayDate);
    Console.WriteLine(date);
}





--Amit





--Amit


1.始终将SQL中的日期时间存储为UTC
2.参见#1
3.将日期下移到客户端,并使用 javascript [ ^ ] Date对象可以根据语言环境和TZ对其进行格式化
4.参见#1

存储UTC可处理DST开关并使所有内容保持一致

我特别的小负担是必须阅读美国日期-在我的世界中,它们每年只正确12次
1. ALWAYS ALWAYS store datetimes in SQL as UTC
2. See #1
3. Move the date down to the client and let the javascript[^] Date object format it for you according to locale and TZ
4. See #1

Storing UTC handles DST switches and makes everything consistent

My particular bugbear is having to read US dates - in my world, they''re only right 12 times a year


请参阅下面的给定链接,然后选择合适的日期格式

http://www.sqlusa.com/bestpractices2005/centurydateformat/ [ http://www.sql-server-helper.com/tips/date-formats.aspx [^ ]
See below given links and choose your approriate dateformat

http://www.sqlusa.com/bestpractices2005/centurydateformat/[^]

http://www.sql-server-helper.com/tips/date-formats.aspx[^]


这篇关于如何在网站上显示日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆