admin管理员组文章数量:1332345
I have some JavaScript code that I'm trying to pass to my web service. My JavaScript code is supposed to send a date in UTC format. Locally, the time that I generated my code at was at 12:30:43 pm. When I executed my JavaScript code, the following date/time was generated:
2012-06-03T20:30:43.000Z
That date/time was generated from this code:
var now = new Date();
var utcDate = new Date(
now.getUTCFullYear(),
now.getUTCMonth(),
now.getUTCDate(),
now.getUTCHours(),
now.getUTCMinutes(),
now.getUTCSeconds()
);
When I pass the date/time from JavaScript back to my web service, it is serialized as shown here:
20120603163043
That looks correct to me at this point. I then need to take that string and convert it to a date/time in C#. In an attempt to do that, I'm using the following C# code:
DateTime _value = DateTime.MinValue;
DateTime.TryParseExact(value, "yyyyMMddHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out _value)
When that happens, I get the following date/time. 6/3/2012 12:30:43 PM
What am I doing wrong? I was expecting the date/time to be 6/3/2012 4:30:43 PM
I have some JavaScript code that I'm trying to pass to my web service. My JavaScript code is supposed to send a date in UTC format. Locally, the time that I generated my code at was at 12:30:43 pm. When I executed my JavaScript code, the following date/time was generated:
2012-06-03T20:30:43.000Z
That date/time was generated from this code:
var now = new Date();
var utcDate = new Date(
now.getUTCFullYear(),
now.getUTCMonth(),
now.getUTCDate(),
now.getUTCHours(),
now.getUTCMinutes(),
now.getUTCSeconds()
);
When I pass the date/time from JavaScript back to my web service, it is serialized as shown here:
20120603163043
That looks correct to me at this point. I then need to take that string and convert it to a date/time in C#. In an attempt to do that, I'm using the following C# code:
DateTime _value = DateTime.MinValue;
DateTime.TryParseExact(value, "yyyyMMddHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out _value)
When that happens, I get the following date/time. 6/3/2012 12:30:43 PM
What am I doing wrong? I was expecting the date/time to be 6/3/2012 4:30:43 PM
Share Improve this question asked Jun 3, 2012 at 16:48 JavaScript DeveloperJavaScript Developer 4,00811 gold badges43 silver badges46 bronze badges3 Answers
Reset to default 3The result you get is correct, but please check the Kind
property of your DateTime
. You'll notice it's not set to UTC
but to Local
.
You can use DateTimeStyles.AdjustToUniversal
to generate a DateTime
with Kind
set to UTC
.
DateTime dateTime;
DateTime.TryParseExact(
value,
"yyyyMMddHHmmss",
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal,
out dateTime);
See it working on ideone.
If you just want to serialize using (e.g. webAPI) I find JS toISOString
very useful and patible.
//JAVASCRIPT
var d = new Date();
$toPut.dateTime = d.toISOString();
$toPut.put()
//C#
[Put("/setup/dateTime"), HttpPut]
public HttpResponseMessage SetDateTime([FromBody]DateTime dateTimeSettings )
That way you can keep your data structure and not deal with parsing.
You must pass date parameter to web service as UTC format and then use TryParse method to convert it to C# date object.
Try this:
//Javascript
var now = new Date(); //just like: 'Thu, 21 Mar 2013 12:44:40 GMT'
var utcNowString = now.toUTCString(); //pass this parameter to your web service
And this is C# code:
DateTime date;
DateTime.TryParse(jsDateString, out date); //parsed as: '21.03.2013 14:44:40'
Hope this helps.
本文标签: Parsing DateandTimes from JavaScript to CStack Overflow
版权声明:本文标题:Parsing Date-and-Times from JavaScript to C# - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742326531a2453838.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论