如何将公历日转换为公历日期
How to convert gregorian days to gregorian date
我在我的 table 中存储了公历日期和一些其他数据。
这些天我通过以下 Erlang 命令获得:
{Date, _} = calendar:now_to_datetime(now()).
GDays = calendar:date_to_gregorian_days(Date).
我们以这个值为例:GDays = 736202.
我用 ErlyDTL 在 Cowboy 中建立了一个网站以供参考。
现在我想以日期格式显示这些公历。 (2015 年 8 月 28 日).
我使用以下代码从我认为的列表中获取数据:
{% for item in list %}
{{item.1}} <br/>
{% endfor %}
我尝试了以下命令 {{item.1|date:" D d M Y"}}
但出现错误:
Unexpected date parameter: 736202
现在如何将公历日期转换为 erlyDTL 或 javascript 中的日期时间?
提前致谢
根据此处的文档:http://www.erlang.org/doc/man/calendar.html
Erlang 以 0 月 1 日这一纪元为基础,并提供了一个示例:1970 年 1 月 1 日是 719528。所以纪元是第 0 天。
以下函数将 Date 对象转换为公历日期并返回。他们 return 根据文档中的单个示例更正值,但将 736202 转换为 2015 年 8 月 27 日而不是 8 月 28 日。也许您使用的是 UTC 而不是本地时间。不管怎样,我想这里已经足够你解决了。
/* @param {Date} [date] - Date object to be converted
** @returns {number} - whole days since 1 January 0 to d
**
** epoch date must set year separately as in many implementations
** new Date(0,0,1) returns 1 Jan 1900, not 1 Jan 0000
*/
function dateToGregorianDays(date) {
// Create a Date object for 0000-Jan-01 (months are zero based)
var epoch = new Date(0,0,1);
// Set the epoch to year 0 as in the above some browsers will
// create a date for 1900 not 0, even though 0 was passed in
epoch.setFullYear(0);
// Copy the passed in Date so it's not modified by next step
var e = new Date(+date);
// Set the time part of the copied date to 00:00:00, which
// helps to calculate whole days
e.setHours(0,0,0,0);
// In mathematic operations, dates are converted to their time value
// which is milliseconds, so get the difference in milliseconds between
// the two dates and divide by milliseconds per day. Round to remove
// fractional parts caused occasionally over daylight saving boundaries
// to get whole day count between the two dates and return it
return Math.round((e - epoch)/8.64e7);
}
/* @param {number} [days] - Gregorian day number
** @returns {Date} - Based on whole days since 1 January 0
**
** 0 -> 1 Jan 0000
** 719528 -> 1 Jan 1970
*/
function gregorianDaysToDate(days) {
// Create a date for 0000-Jan-01
var epoch = new Date(0,0,1);
epoch.setFullYear(0);
// Add the number of days to the date
// epoch.getDate() could be replaced by 1 since that's what
// the date was set to just above
epoch.setDate(epoch.getDate() + days);
// Return the date
return epoch;
}
/* Simple function to return a date string as dd-MMM-yyyy
** @param {Date} [date] - Date to format
** @returns {string} - formatted string for date
*/
function formatDateDMY(date) {
// Month names
var months = ['Jan','Feb','Mar','Apr','May','Jun',
'Jul','Aug','Sep','Oct','Nov','Dec'];
// Add leading zero to single digit days
// Get the month name for the month (zero indexed, 0 is Jan)
// Add leading zeros to years with less than 4 digits
// Use '-' as separator
return ('0' + date.getDate()).slice(-2) + '-' +
months[date.getMonth()] + '-' +
('000' + date.getFullYear()).slice(-4);
}
// Create an alias for the above function to save typing
var fd = formatDateDMY;
// Gregorian days for 01-Jan-1970
document.write(dateToGregorianDays(new Date(1970,0,1)) + '<br>'); // 719528
// Gregorian calendar date for 719528 formatted as dd-MMM-yyyy
document.write(fd(gregorianDaysToDate(719528)) + '<br>') // 01 Jan 1970
// Gregorian calendar date for 0 formatted as dd-MMM-yyyy
document.write(fd(gregorianDaysToDate(0)) + '<br>'); // 01 Jan 0000
// Gregorian calendar date for 736202 formatted as dd-MMM-yyyy
document.write(fd(gregorianDaysToDate(736202)) + '<br>'); // 27 Aug 2015
// Gregorian days for 28-Aug-2015
document.write(dateToGregorianDays(new Date(2015,7,28))); // 736203
这是我需要的算法。我在 javascript 中以一种非常简单的方式做到了,现在已经足够好了,所以不要挑剔 ;)。
不支持闰年
<html>
<head>
<script>
function date(n){
// Calculate the year.
var year = Math.round(n / 365.2425 - 0.5);
// Get the remainder of the year calculation.
var dec = Math.round(((n * 10000) / 365.2425) - (year * 10000));
// Multiply remainder with 365.2425.
var day = Math.round((dec * 365.25) / 10000);
// Search which month and day it is.
if(day <= 30){
return day.toString().concat(" januari ").concat(year);
}else if(day <= 58){
return (day - 30).toString().concat(" februari ").concat(year.toString());
}else if(day <= 89){
return (day - 58).toString().concat(" maart ").concat(year.toString());
}else if(day <= 119){
return (day - 89).toString().concat(" april ").concat(year.toString());
}else if(day <= 150){
return (day - 119).toString().concat(" mei ").concat(year.toString());
}else if(day <= 180){
return (day - 150).toString().concat(" juni ").concat(year.toString());
}else if(day <= 211){
return (day - 180).toString().concat(" juli ").concat(year.toString());
}else if(day <= 242){
return (day - 211).toString().concat(" augustus ").concat(year.toString());
}else if(day <= 272){
return (day - 242).toString().concat(" september ").concat(year.toString());
}else if(day <= 303){
return (day - 272).toString().concat(" oktober ").concat(year.toString());
}else if(day <= 333){
return (day - 303).toString().concat(" november ").concat(year.toString());
}else if(day <= 364){
return (day - 333).toString().concat(" december ").concat(year.toString());
}else{
return "error";
}
}
</script>
</head>
<body>
<script>
document.write(date(736205));
</script>
</body>
</html>
感谢大家的帮助。
我在我的 table 中存储了公历日期和一些其他数据。 这些天我通过以下 Erlang 命令获得:
{Date, _} = calendar:now_to_datetime(now()).
GDays = calendar:date_to_gregorian_days(Date).
我们以这个值为例:GDays = 736202.
我用 ErlyDTL 在 Cowboy 中建立了一个网站以供参考。 现在我想以日期格式显示这些公历。 (2015 年 8 月 28 日).
我使用以下代码从我认为的列表中获取数据:
{% for item in list %}
{{item.1}} <br/>
{% endfor %}
我尝试了以下命令 {{item.1|date:" D d M Y"}}
但出现错误:
Unexpected date parameter: 736202
现在如何将公历日期转换为 erlyDTL 或 javascript 中的日期时间?
提前致谢
根据此处的文档:http://www.erlang.org/doc/man/calendar.html
Erlang 以 0 月 1 日这一纪元为基础,并提供了一个示例:1970 年 1 月 1 日是 719528。所以纪元是第 0 天。
以下函数将 Date 对象转换为公历日期并返回。他们 return 根据文档中的单个示例更正值,但将 736202 转换为 2015 年 8 月 27 日而不是 8 月 28 日。也许您使用的是 UTC 而不是本地时间。不管怎样,我想这里已经足够你解决了。
/* @param {Date} [date] - Date object to be converted
** @returns {number} - whole days since 1 January 0 to d
**
** epoch date must set year separately as in many implementations
** new Date(0,0,1) returns 1 Jan 1900, not 1 Jan 0000
*/
function dateToGregorianDays(date) {
// Create a Date object for 0000-Jan-01 (months are zero based)
var epoch = new Date(0,0,1);
// Set the epoch to year 0 as in the above some browsers will
// create a date for 1900 not 0, even though 0 was passed in
epoch.setFullYear(0);
// Copy the passed in Date so it's not modified by next step
var e = new Date(+date);
// Set the time part of the copied date to 00:00:00, which
// helps to calculate whole days
e.setHours(0,0,0,0);
// In mathematic operations, dates are converted to their time value
// which is milliseconds, so get the difference in milliseconds between
// the two dates and divide by milliseconds per day. Round to remove
// fractional parts caused occasionally over daylight saving boundaries
// to get whole day count between the two dates and return it
return Math.round((e - epoch)/8.64e7);
}
/* @param {number} [days] - Gregorian day number
** @returns {Date} - Based on whole days since 1 January 0
**
** 0 -> 1 Jan 0000
** 719528 -> 1 Jan 1970
*/
function gregorianDaysToDate(days) {
// Create a date for 0000-Jan-01
var epoch = new Date(0,0,1);
epoch.setFullYear(0);
// Add the number of days to the date
// epoch.getDate() could be replaced by 1 since that's what
// the date was set to just above
epoch.setDate(epoch.getDate() + days);
// Return the date
return epoch;
}
/* Simple function to return a date string as dd-MMM-yyyy
** @param {Date} [date] - Date to format
** @returns {string} - formatted string for date
*/
function formatDateDMY(date) {
// Month names
var months = ['Jan','Feb','Mar','Apr','May','Jun',
'Jul','Aug','Sep','Oct','Nov','Dec'];
// Add leading zero to single digit days
// Get the month name for the month (zero indexed, 0 is Jan)
// Add leading zeros to years with less than 4 digits
// Use '-' as separator
return ('0' + date.getDate()).slice(-2) + '-' +
months[date.getMonth()] + '-' +
('000' + date.getFullYear()).slice(-4);
}
// Create an alias for the above function to save typing
var fd = formatDateDMY;
// Gregorian days for 01-Jan-1970
document.write(dateToGregorianDays(new Date(1970,0,1)) + '<br>'); // 719528
// Gregorian calendar date for 719528 formatted as dd-MMM-yyyy
document.write(fd(gregorianDaysToDate(719528)) + '<br>') // 01 Jan 1970
// Gregorian calendar date for 0 formatted as dd-MMM-yyyy
document.write(fd(gregorianDaysToDate(0)) + '<br>'); // 01 Jan 0000
// Gregorian calendar date for 736202 formatted as dd-MMM-yyyy
document.write(fd(gregorianDaysToDate(736202)) + '<br>'); // 27 Aug 2015
// Gregorian days for 28-Aug-2015
document.write(dateToGregorianDays(new Date(2015,7,28))); // 736203
这是我需要的算法。我在 javascript 中以一种非常简单的方式做到了,现在已经足够好了,所以不要挑剔 ;)。 不支持闰年
<html>
<head>
<script>
function date(n){
// Calculate the year.
var year = Math.round(n / 365.2425 - 0.5);
// Get the remainder of the year calculation.
var dec = Math.round(((n * 10000) / 365.2425) - (year * 10000));
// Multiply remainder with 365.2425.
var day = Math.round((dec * 365.25) / 10000);
// Search which month and day it is.
if(day <= 30){
return day.toString().concat(" januari ").concat(year);
}else if(day <= 58){
return (day - 30).toString().concat(" februari ").concat(year.toString());
}else if(day <= 89){
return (day - 58).toString().concat(" maart ").concat(year.toString());
}else if(day <= 119){
return (day - 89).toString().concat(" april ").concat(year.toString());
}else if(day <= 150){
return (day - 119).toString().concat(" mei ").concat(year.toString());
}else if(day <= 180){
return (day - 150).toString().concat(" juni ").concat(year.toString());
}else if(day <= 211){
return (day - 180).toString().concat(" juli ").concat(year.toString());
}else if(day <= 242){
return (day - 211).toString().concat(" augustus ").concat(year.toString());
}else if(day <= 272){
return (day - 242).toString().concat(" september ").concat(year.toString());
}else if(day <= 303){
return (day - 272).toString().concat(" oktober ").concat(year.toString());
}else if(day <= 333){
return (day - 303).toString().concat(" november ").concat(year.toString());
}else if(day <= 364){
return (day - 333).toString().concat(" december ").concat(year.toString());
}else{
return "error";
}
}
</script>
</head>
<body>
<script>
document.write(date(736205));
</script>
</body>
</html>
感谢大家的帮助。