我需要在javascript中实现这个c#方法.需要帮助吗?
本文关键字:方法 帮助 实现 javascript | 更新日期: 2023-09-27 18:08:36
我在c#.net的代码中使用了这个方法。我会将从数据库获取的日期传递到该方法的参数中,然后以非正式格式写入日期。现在我需要在jquery代码和
中实现同样的东西我需要在这里使用这个函数。在我的JavaScript文件中,我将日期作为字符串。现在的问题是,我不知道如何将字符串转换为日期!那么我应该如何使用该日期并将其传递给这个函数?然后进行转换,例如.....(int) s.t oltoseconds;
给我一些想法,我怎么才能实现我想要的。我是JQuery的新手。
static string GetInformalDate(DateTime d)
{
// 1.
// Get time span elapsed since the date.
TimeSpan s = DateTime.Now.Subtract(d);
// 2.
// Get total number of days elapsed.
int dayDiff = (int)s.TotalDays;
// 3.
// Get total number of seconds elapsed.
int secDiff = (int)s.TotalSeconds;
// 4.
// Don't allow out of range values.
if (dayDiff < 0)
{
return null;
}
// 5.
// Handle same-day times.
if (dayDiff == 0)
{
// A.
// Less than one minute ago.
if (secDiff < 60)
{
return "Just Now";
}
// B.
// Less than 2 minutes ago.
if (secDiff < 120)
{
return "1 minute ago";
}
// C.
// Less than one hour ago.
if (secDiff < 3600)
{
return string.Format("{0} minutes ago",
Math.Floor((double)secDiff / 60));
}
// D.
// Less than 2 hours ago.
if (secDiff < 7200)
{
return "1 hour ago";
}
// E.
// Less than one day ago.
if (secDiff < 86400)
{
return string.Format("{0} hours ago",
Math.Floor((double)secDiff / 3600));
}
}
// 6.
// Handle previous days, months and years.
if (dayDiff == 1)
{
return "Yesterday";
}
if (dayDiff < 7)
{
return string.Format("{0} days ago",
dayDiff);
}
if (dayDiff < 31)
{
return string.Format("{0} week(s) ago",
Math.Ceiling((double)dayDiff / 7));
}
if (dayDiff < 365)
{
return string.Format("{0} month(s) ago", Math.Ceiling((double)dayDiff / 31));
}
else
{
return string.Format("{0} year(s) ago", Math.Ceiling((double)dayDiff / 365));
}
return null;
}
@ IAbstractDownvoteFactory:我在那个聊天室。如果你还在这儿等着,你能来吗?需要帮助吗?
请问还有谁能帮我吗?
示例
function GetUnformalDate(d) {
// 1.
// Get time span elapsed since the date.
var s = new Date().getTime() - d.getTime();
// 2.
// Get total number of days elapsed.
var dayDiff = (s / 86400000) | 0;
// 3.
// Get total number of seconds elapsed.
var secDiff = s / (1000);
// 4.
// Don't allow out of range values.
if (dayDiff < 0) {
return null;
}
// 5.
// Handle same-day times.
if (dayDiff == 0) {
// A.
// Less than one minute ago.
if (secDiff < 60) {
return "Just Now";
}
// B.
// Less than 2 minutes ago.
if (secDiff < 120) {
return "1 minute ago";
}
// C.
// Less than one hour ago.
if (secDiff < 3600) {
return Math.floor(secDiff / 60) + " minutes ago";
}
// D.
// Less than 2 hours ago.
if (secDiff < 7200) {
return "1 hour ago";
}
// E.
// Less than one day ago.
if (secDiff < 86400) {
return Math.floor(secDiff / 3600) + " hours ago";
}
}
// 6.
// Handle previous days, months and years.
if (dayDiff == 1) {
return "Yesterday";
}
if (dayDiff < 7) {
return dayDiff + " days ago";
}
if (dayDiff < 31) {
return Math.ceil(dayDiff / 7) + " week(s) ago";
}
if (dayDiff < 365) {
return Math.ceil(dayDiff / 31) + " month(s) ago";
}
else {
return Math.ceil(dayDiff / 365) + " year(s) ago";
}
return null;
}
作为日期字符串,我们必须解析它:
var digits = "29-07-2011 12:51:33".match(/'d+/g);
var date = new Date(digits[2], digits[1], digits[0], digits[3], digits[4], digits[5])