如何使用Javascript日期时间转换为C#日期时间? [英] How to convert Javascript datetime to C# datetime?

查看:112
本文介绍了如何使用Javascript日期时间转换为C#日期时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在读,如果你想从JavaScript日期转换为你应该使用的getTime(),然后将该结果添加到C#日期时间。

I have been reading that if you want to convert from JavaScript dates to C# dates you should use getTime() and then add that result to a C# DateTime.

假如我有这个JavaScript时间:

Suppose I have this JavaScript time:

Date {Tue Jul 12 2011 16:00:00 GMT-0700 (Pacific Daylight Time)}

它呈现为 1310522400000 毫秒

var a = new DateTime(1970, 01, 01).AddMilliseconds(1310522400000);

// result
7/13/2011 2:00:00 AM

所以,这是错误的。我不知道我需要做的。

So this is wrong. I am not sure what I need to do.

推荐答案

首先,在您需要的格式使用以下功能在JavaScript中创建一个字符串

First create a string in your required format using the following functions in JavaScript

var date = new Date();
var day = date.getDay();        // yields day
var month = date.getMonth();    // yields month
var year = date.getFullYear();  // yields year
var hour = date.getHours();     // yields hours 
var minute = date.getMinutes(); // yields minutes
var second = date.getSeconds(); // yields seconds

// After this construct a string with the above results as below
var time = day + "/" + month + "/" + year + " " + hour + ':' + minute + ':' + second; 

通过这个字符串codebehind功能,并接受它作为一个字符串parameter.Use的 DateTime.ParseExact()在codebehind这个字符串转换成的DateTime 如下,

Pass this string to codebehind function and accept it as a string parameter.Use the DateTime.ParseExact() in codebehind to convert this string to DateTime as follows,

DateTime.ParseExact(YourString, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);

希望这有助于...

Hope this helps...

这篇关于如何使用Javascript日期时间转换为C#日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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