【问题标题】:Javascript Date To C# Webservice ErrorJavascript 日期到 C# Web 服务错误
【发布时间】:2015-08-17 03:54:53
【问题描述】:

我一直在尝试将 javascript 日期转换为 c# datetime 格式以插入到 microsoft sql 数据库中。

这是我目前拥有的代码:

Javascript 方面:

var now     = new Date();
now =  now.toUTCString();

C#端:

//time variable is in DateTime Format

Console.WriteLine("Before: "+time);
String timeString = Convert.ToString(time);

Console.WriteLine("Convert: "+timeString);

DateTime newDT =  DateTime.ParseExact(timeString, "yyyy-MM-dd HH:mm:ss:FFF",   CultureInfo.InvariantCulture);

Console.WriteLine("After: "+newDT);

错误消息:

System.FormatException: String was not recognized as a valid DateTime.
at System.DateTimeParse.ParseExact(String s, String format,    DateTimeFormatInfo dtfi, DateTimeStyles style)
at -classfile&method name omiited-(DateTime time)

调试信息:

 Before: 17/8/2015 11:43:48 AM
 Convert: 17/8/2015 11:43:48 AM

我很确定 ParseExact 中的“timeString”是错误的。

关于如何解决这个问题的任何想法? 我想要的结果格式是这种格式: 2015-07-27 14:24:23.853 。谢谢!

【问题讨论】:

  • 这是来自网络请求的服务器方法吗?如果是这样,您将获得什么数据。 timetimeString 应该模拟这个吗?还是我完全错过了标记? (你的 JS 和 C# 有什么联系?它们是如何通信的?)
  • 非常不清楚您的问题是什么。听起来你已经想出了解析 JS 日期(你的代码似乎也不是最好的 - stackoverflow.com/questions/1877788/…),可能你的问题是“如何将 DateTime 打印为 ISO8601 格式”...

标签: javascript c# parsing datetime


【解决方案1】:

Date 对象上的toUTCString 函数返回一个 RFC2822 格式的值,例如 "Mon, 17 Aug 2015 05:25:53 GMT"。这与您在 C# 代码中指定的 "yyyy-MM-dd HH:mm:ss:FFF" 格式不一致。

你有两个选择。我推荐第二个。

  1. 更改 C# 代码以匹配传递的格式。 "r" 标准格式与 RFC2822 匹配。

    string s = "Mon, 17 Aug 2015 05:25:53 GMT";
    DateTime dt = DateTime.ParseExact(s, "r", CultureInfo.InvariantCulture,
                      DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);
    
  2. 在两边都使用更清晰的格式。对于大多数事情,我推荐 ISO8601 格式。在 JavaScript 中,使用toISOString 方法获取类似于"2015-08-17T05:25:43.780Z" 的日期。然后将您的 C# 代码更改为:

    var s = "2015-08-17T05:25:43.780Z";
    DateTime dt = DateTime.ParseExact(s, "yyyy-MM-dd'T'HH:mm:ss.FFFZ",
                      CultureInfo.InvariantCulture,  DateTimeStyles.RoundtripKind);
    

【讨论】:

  • 感谢您的详细解释!
【解决方案2】:

试试这个:- https://dotnetfiddle.net/wLSaYD

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

namespace DateParse
{
    class Program
    {
        static void Main(string[] args)
        {
            var time = "17/8/2015 11:43:48 AM";

            Console.WriteLine("Before: " + time);

            DateTime newDT = DateTime.ParseExact(time, "dd/M/yyyy hh:mm:ss tt", null);

            Console.WriteLine("After: "+newDT.ToString("yyyy-MM-dd HH:mm:ss.fff"));
        }
    }
}

【讨论】:

  • newDT.ToString("yyyy-MM-dd HH:mm:ss.fff") 是解决这个问题的方法。非常感谢!
【解决方案3】:

17/8/2015 11:43:48 AM反对dd/M/yyyy HH:mm:ss tt格式,使用tostring格式

DateTime newDT = DateTime.ParseExact(timeString, "dd/M/yyyy HH:mm:ss tt", CultureInfo.InvariantCulture);
Console.WriteLine("After: "+newDT.ToString("yyyy-MM-dd HH:mm:ss.fff"));

【讨论】:

  • 嗨,我想转换为“yyyy-MM-dd HH:mm:ss:FFF”,有什么想法吗?
猜你喜欢
  • 2017-06-06
  • 1970-01-01
  • 1970-01-01
  • 2011-05-29
  • 1970-01-01
  • 2023-03-09
  • 2019-10-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多