首页 新闻 会员 周边

C# 如何改变一段语句中日期字段的格式

0
悬赏园豆:20 [已解决问题] 解决于 2016-09-14 11:22

有个面试题是这样的,要求写一个C#函数,把一段语句中日期字段的格式

从 MM/DD/YYYY 改为DD/MM/YYYY

也就是说 输入:

"Last time it rained was on 07/25/2013 and today is 08/09/2013."

 

要求输出

"Last time it rained was on 25/07/2013 and today is 09/08/2013."

 

我在规定的时间内,硬是没有做出来,请问有前辈可以指点一二吗

 

谢谢了

新西兰程序员的主页 新西兰程序员 | 初学一级 | 园豆:3
提问于:2016-09-13 15:00
< >
分享
最佳答案
0

正则匹配出来。然后处理完在替换。

收获园豆:20
长蘑菇星人 | 小虾三级 |园豆:1832 | 2016-09-13 15:07

正解,好结贴了

thomaschen | 园豆:7 (初学一级) | 2016-09-13 17:21

@thomaschen: 麻烦能给出一个例子吗,我尝试了半天,也没弄出来。以下是我的代码,没有任何改变

 

  static void Main(string[] args)
        {

            string newStr = UpdateDateFormat("Last time it rained was on 07/25/2013 and today is 08/09/2013");
            Console.WriteLine(newStr);
        }



        public static string UpdateDateFormat(string a)
        {

            string[] str1 = a.Split(' ');

            var pattern = @"\\b(?<month>\\d{1,2})/(?<day>\\d{1,2})/(?<year>\\d{2,4})\\b";

            Regex rgx = new Regex(pattern, RegexOptions.None,TimeSpan.FromMilliseconds(150));

            foreach (var item in str1)
            {

                var newItem = "";

                if (item.IndexOf('/') > 0)
                {

                    //a = a.Replace(item, Convert.ToDateTime(item).ToString("dd/MM/yyyy"));
                    newItem = Regex.Replace(item, pattern, "${day}-${month}-${year}", RegexOptions.None, TimeSpan.FromMilliseconds(150));

                    a = a.Replace(item, newItem);

                }


            }

            return a;

        }
新西兰程序员 | 园豆:3 (初学一级) | 2016-09-14 05:54

@thomaschen: 最后是在csdn上的一个高人给出了答案

  public static string UpdateDateFormat(string a)
        {

            string[] str1 = a.Split(' ');

            var pattern = @"(\d+)/(\d+)/(\d+)";

            Regex rgx = new Regex(pattern, RegexOptions.None,TimeSpan.FromMilliseconds(150));

            foreach (var item in str1)
            {

                var newItem = "";

                if (item.IndexOf('/') > 0)
                {

                    //a = a.Replace(item, Convert.ToDateTime(item).ToString("dd/MM/yyyy"));
                    newItem = Regex.Replace(item, pattern, "$2/$1/$3", RegexOptions.None, TimeSpan.FromMilliseconds(150));

                    a = a.Replace(item, newItem);

                }


            }

            return a;

        }

 

 

新西兰程序员 | 园豆:3 (初学一级) | 2016-09-14 11:21
其他回答(6)
0

你这个是怎么判断它是不是日期的?输入只有一串字符串么

Boblim | 园豆:492 (菜鸟二级) | 2016-09-13 15:05

如果是字符串的话,你可以直接根据‘/’位置来转换日期

支持(0) 反对(0) Boblim | 园豆:492 (菜鸟二级) | 2016-09-13 15:08
0

这关键是怎么找时间格式了吧?找到了DateTime转一下就行?

顾晓北 | 园豆:10844 (专家六级) | 2016-09-13 15:09
0

DateTime.TryParse

DateTime.ToString

Daniel Cai | 园豆:10424 (专家六级) | 2016-09-13 15:16
0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication28
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "Last time it rained was on 07/25/2013 and today is 08/09/2013.";

            string[] str1 = str.Split(' ');

            foreach (var item in str1)
            {
                DateTime dt;
                if (DateTime.TryParse(item,out dt))
                {
                    //"Last time it rained was on 25/07/2013 and today is 09/08/2013."
                    str= str.Replace(item,dt.ToString("dd/MM/yyyy"));
                    Console.WriteLine(item);
                }
            }
            Console.WriteLine(str);
        }
    }
}

 

需要格局 | 园豆:2145 (老鸟四级) | 2016-09-13 15:54

不工作啊,字符串完全没变,我测试了

支持(0) 反对(0) 新西兰程序员 | 园豆:3 (初学一级) | 2016-09-14 05:05

@新西兰程序员:黑底白字,明明在这里写着,难道我会骗你?

支持(0) 反对(0) 需要格局 | 园豆:2145 (老鸟四级) | 2016-09-22 09:02
0

使用正则表达式把时间找出来,然后替换。

CodeHsu | 园豆:5468 (大侠五级) | 2016-09-13 16:23

可否给出个能够测试通过的例子

我试了,搞不定呀

支持(0) 反对(0) 新西兰程序员 | 园豆:3 (初学一级) | 2016-09-14 06:21
0

我是觉得,正则表达式有点难学。高手们能否给个学习的方法?

德展自动化 | 园豆:206 (菜鸟二级) | 2016-09-14 08:36
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册