gpt4 book ai didi

javascript - 将 AngularJS 中的时间戳格式化为自定义要求

转载 作者:行者123 更新时间:2023-11-30 10:09:04 25 4
gpt4 key购买 nike

我有一个新帖子返回的一串数字,在提交时以 new Date().getTime() 格式加了时间戳。我希望放置一个持续时间戳,例如 (new Date().getTime() of submitted post - current date time) 或者本质上是从提交帖子时起经过的日期时间。

如果<一个月的持续时间(但一个月的定义各不相同,有些二月有 28 天?),减法将返回分钟 小时 天前。不确定是否有全局时区调整。如果它 > 一个月,则将其显示为 月份(一月、三月、十二月等)2014 年,例如不涉及任何持续时间。

我听说过 moment.JS 但不确定上面是否有这样的定制。我应该如何处理转换?如果有示例代码可供引用,我们将不胜感激。

非常感谢!

最佳答案

I have a string of numbers returned from new Date().getTime()

这将是一个数字,表示自 1970-01-01T00:00:00Z(ECMAScript 纪元)以来的毫秒数和执行代码的 UTC 时间。

and saved as that in the back end. I wish to put a duration timestamp like (new Date().getTime() - now()).

如果 now()Date.now() 的简写,那么该表达式的结果将为零 (0),因为两个表达式将表示同一时刻及时。

However I want filter to show it as mins, hours, days ago

将以毫秒为单位的值转换为天、小时、分钟、秒的值很简单。

if its less than a month (but definition of a month varies, Feb sometimes has 28 days?) Im not sure if the timestamp string accounts for global timezone adjustments.

Date 对象的核心时间值是自纪元以来的 UTC 毫秒数。日期对象还有一个由系统设置确定的偏移量,用于计算本地时间值。有获取 UTC 值的 UTC 方法和获取本地值的非 UTC 方法。

If its more than a month, to show it as month (Jan, Mar, Dec etc) and the year 2014 for example without any duration involved.

两个时间值相减的结果是一个标量数,它没有月或年的概念,它只是表示持续时间,而不是时刻。因此,只有在有特定起点(纪元)和方向的情况下,将数字转换为“月”才有意义。

例如

1 月 1 日 + 31 天 -> 2 月 1 日所以一个月,但是 6 月 1 日 + 31 天 -> 7 月 2 日所以 1 个月零 1 天。

1 月 1 日 - 31 天 -> 12 月 1 日即一个月,6 月 1 日 - 31 天 -> 5 月 1 日即 1 个月。

I heard of moment JS but not sure if theres such a customization for duration

我对 moment.js 了解不多,只是我从来没有发现它的需要。

How should I approach the converting?

首先阐明您的要求。也许您想表示从特定时间点(例如现在或 2014 年 6 月 1 日或其他时间)开始的持续时间,如月、日、小时等?

上面有些是微不足道的,有些是相当困难的,取决于行政规则,例如是2012年2月29日+1年2013年2月28日还是2013年3月1日?

编辑

下面的示例代码是确定两个日期之间的年、月和日的一种相当可靠的方法。它还执行时差小于 1 天(即 8.64e7 毫秒)的小时:分钟:秒。

它可能看起来像很多代码,其中大部分是年、月、日部分。

/**
* Return the number of days in the month for the given year.
* Month is calendar month (Jan=1, Feb=2, etc.).
* @param {number} [month]
* @param {number} [year]
* @returns {number}
* Default is current month, current year
*/
function getDaysInMonth(month, year) {
var d = new Date();
d.setHours(12,0,0,0);
if (typeof month == 'undefined') month = d.getMonth() + 1;
if (typeof year == 'undefined') year = d.getFullYear();
d.setFullYear(year, month, 0);
return d.getDate();
}

/**
* Add years to a given Date, modifies the Date.
* If adding years to 29 Feb rolls over to March 1,
* then the date is set to 28 Feb.
* @param {Date} date - Date to add years to
* @param {number} years - Number of years to add
* @returns {Date} Modified original date object
*/
function addYears(date, years) {
var m = date.getMonth();
date.setFullYear(date.getFullYear() + years);

// Deal with leap year: if 29 Feb -> 1 Mar set back to 28 Feb
if (date.getMonth() != m) {
date.setDate(0);
}
return date;
}

/**
* Add months to a given Date, modifies the Date.
* If adding months causes the date to roll over an extra month,
* the date is set to last day of previous month.
*
* e.g. 31 May + 1 month -> 30 June, not 1 July
* 31 Jan + 1 month -> 28 Feb or 29 Feb if leap year
* @param {Date} date - Date to add months to
* @param {number} months - Number of months to add
* @returns {Date} Modified original
*/
function addMonths(date, months) {
var n = date.getDate();
date.setMonth(date.getMonth() + months);

if (date.getDate() != n) {
date.setDate(0);
}
return date;
}

/**
* Add days to a given Date, modifies the Date.
* @param {Date} date - Date to add days to
* @param {number} days - Number of days to add
* @returns {Date} Modified original
*/
function addDays(date, days) {
date.setDate(date.getDate() + days);
return date;
}

/**
* Convert seconds to hh:mm:ss
* @param {number|string} secs
* @returns {number}
*/
function secondsToHMS(secs) {
function z(n){return (n<10?'0':'') + n;}
var sign = secs < 0? '-':'';
secs = Math.abs(secs);
return sign + z(secs/3600 |0) + ':' + z((secs%3600) / 60 |0) + ':' + z(secs%60);
}

/**
* Get the time between two dates as years, months and days.
* For startDate of 29 Feb, whole year is 28 Feb in following year or
* 29 Feb if endDate is a leap year. Some systems use 1 Mar.
* @param {Date} startDate
* @param {Date} [endDate]
* @returns {string} 'y years, m moths and d days'
* If endDate not provided, current date is used.
* endDate must be after startDate.
*/
function getAge(startDate, endDate) {

// Return undefined if start date is after end date
if (startDate > endDate) return;

var d, d0, d1, years, months, days;
var startMonth = startDate.getMonth();

d1 = endDate? new Date(+endDate) : new Date();
d1.setHours(0,0,0,0);

d = new Date(+startDate);
d.setHours(0,0,0,0);
d0 = new Date(+d);

years = d1.getFullYear() - d.getFullYear();
addYears(d, years);

if (d > d1) {
--years;
d = new Date(+d0);
addYears(d, years);
}

months = d1.getMonth() - d.getMonth();

// Deal with -ve month difference
if (months < 0) {
months += 12;

// Deal with months the same and difference < 1 year
} else if (months == 0 && d.getFullYear() != d1.getFullYear()) {
months = 11;
}

addMonths(d, months);

if (d > d1) {
--months;
d = new Date(+d0);
addYears(d, years);
addMonths(d, months);
}

days = d1.getDate() - d.getDate();

if (days < 0 ) {
days += getDaysInMonth(d.getMonth()+1, d.getFullYear());

} else if (days == 0 && d1.getMonth() != d.getMonth()) {
days = getDaysInMonth(d.getMonth()+1, d.getFullYear()) - 1;
}

// Helper to make words plural if num != 1
function s(num, word) {return word + (num == 1? '' : 's')}

return years + s(years, ' year' ) + ', ' +
months + s(months, ' month') + ', ' +
days + s(days, ' day' );
}


// Basic function to parse an ISO 8601 format string as a
// local time. Any timezone is ignored.
// Honours 2 digit years, so 14 is not 1914.
function parseString(s) {
var d = new Date();
b = s.split(/\D+/);
d.setFullYear(b[0], b[1] - 1, b[2]);
d.setHours(b[3] || 0, b[4] || 0, b[5]);
return d;
}


function showDuration() {
var el = document.getElementById('d0');
var now = new Date();

// Convert date string to a Date object
var then = parseString(document.getElementById('i0').value);

// If not a valid date, do nothing
if (!then) return;

// If difference greater than 1 day, show years, months, etc.
// Otherwise, show h:m:s
el.innerHTML = ((now - then) > 8.64e7? getAge(then, now) :
secondsToHMS(Math.abs(then - now)/1000 | 0)) + ' ago.';
}

一些标记:

<input id="i0" value="2014-06-23T20:28:09">
<button onclick="showDuration()">Show duration</button>
<br>
<div id="d0"></div>

关于javascript - 将 AngularJS 中的时间戳格式化为自定义要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27538537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com