new DateTime(DateTime.UtcNow.Ticks, DateTimeKind.Utc).ToString(CultureInfo.InvariantCulture)
new DateTime(DateTime.UtcNow.Ticks, DateTimeKind.Utc).ToString("(UTC zzz)", CultureInfo.InvariantCulture)
DateTime.Now.ToString("G", CultureInfo.CurrentCulture)
怎么将前台的时间传到后台,并且按照CultureInfo.CurrentCulture的形式显示:
前台js中:
var timeZone = (new Date().getTimezoneOffset() / 60) * (-1)
var timeZoneStr = ' (UTC' + timeZone + ':00)';
if (timeZone >= 0) {
timeZoneStr = ' (UTC+' + timeZone + ':00)';
}
var d = new Date();
var modifiedTime = {
Year: d.getFullYear(),
Month: d.getMonth() + 1,
Day: d.getDate(),
Hour: d.getHours(),
Minute: d.getMinutes(),
Second: d.getSeconds(),
TimeZone: timeZoneStr
};
$('#ModifiedTime').val(JSON.stringify(modifiedTime));
然后通过ajax将ModifiedTime传到后台controller中,传递的data:$('form').serialize()。
后台controller中:
var modifiedTimeList = JsonConvert.DeserializeObject<TimeModel>(model.ModifiedTime);
model.ModifiedTime = new DateTime(modifiedTimeList.Year, modifiedTimeList.Month, modifiedTimeList.Day, modifiedTimeList.Hour, modifiedTimeList.Minute, modifiedTimeList.Second).ToString("G", CultureInfo.CurrentCulture) + modifiedTimeList.TimeZone;
后台的ticks值怎么转化成前台时间:
C#中:
homeDetailModel.ExpiredTime=(new TimeSpan(customerApp.License.Units[0].ExpirationTime - new DateTime(1970, 1, 1, 0, 0, 0).Ticks).TotalMilliseconds).ToString();
new TimeSpan(ticks).TotalMilliseconds
js中:
var expiresDate = new Date(parseInt(model[i].ExpiredTime)).format("yyyy/MM/dd");
当然,.format()方法是自己扩充的:
1 String.prototype.format = function(args) { 2 var result = this; 3 if (arguments.length > 0) { 4 if (arguments.length == 1 && typeof(args) == "object") { 5 for (var key in args) { 6 if (args[key] != undefined) { 7 var reg = new RegExp("({" + key + "})", "g"); 8 result = result.replace(reg, args[key]); 9 } 10 } 11 } else { 12 for (var i = 0; i < arguments.length; i++) { 13 if (arguments[i] != undefined) { 14 var reg = new RegExp("({[" + i + "]})", "g"); 15 result = result.replace(reg, arguments[i]); 16 } 17 } 18 } 19 } 20 return result; 21 };