如何从DateTime Dart对象获取一年中的哪几天 [英] How to get day of year, week of year from a DateTime Dart object

查看:353
本文介绍了如何从DateTime Dart对象获取一年中的哪几天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从Dart DateTime对象获取一年中的某天(day1是1月1日),一年中的一周和一年中的月份。

I need to get day of year (day1 is 1rst of january), week of year, and month of year from a dart DateTime object.

我没有查找任何可用的库。有任何想法吗?

I did not find any available library for this. Any idea ?

推荐答案

一年中的一周:

/// Calculates week number from a date as per https://en.wikipedia.org/wiki/ISO_week_date#Calculation
int weekNumber(DateTime date) {
  int dayOfYear = int.parse(DateFormat("D").format(date));
  return ((dayOfYear - date.weekday + 10) / 7).floor();
}

其余的可通过 DateFormat 国际包)。

这篇关于如何从DateTime Dart对象获取一年中的哪几天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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