<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	
	>
<channel>
	<title>
	〈Day.js 計算最近7天、上週、上個月的日期〉的留言	</title>
	<atom:link href="https://www.letswrite.tw/dayjs-last-week-month/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.letswrite.tw/dayjs-last-week-month/</link>
	<description>前端工程師 August 的學習筆記 -- solving problems, in simple ways.</description>
	<lastBuildDate>Sun, 23 Feb 2025 13:52:36 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>
	<item>
		<title>
		留言者: andrew		</title>
		<link>https://www.letswrite.tw/dayjs-last-week-month/#comment-219</link>

		<dc:creator><![CDATA[andrew]]></dc:creator>
		<pubDate>Wed, 28 Apr 2021 02:38:39 +0000</pubDate>
		<guid isPermaLink="false">http://localhost:3001/?p=2288#comment-219</guid>

					<description><![CDATA[上個月的起訖 , 我會用 &lt;a href=&quot;https://day.js.org/docs/en/manipulate/start-of&quot; target=&quot;_blank&quot; rel=&quot;noopener nofollow ugc&quot;&gt;startOf&lt;/a&gt; 跟 &lt;a href=&quot;https://day.js.org/docs/en/manipulate/end-of&quot; target=&quot;_blank&quot; rel=&quot;noopener nofollow ugc&quot;&gt;endOf&lt;/a&gt;

&lt;pre class=&quot;ql-syntax&quot; spellcheck=&quot;false&quot;&gt;// 上個月第一天
 dayjs().subtract(1, &#039;month&#039;).startOf(&#039;month&#039;).toDate();

// 上個月最後一天
 dayjs().subtract(1, &#039;month&#039;).endOf(&#039;month&#039;).toDate();
&lt;/pre&gt;

如果需要 timezone +0 的上個月起迄 , 我會使用 &lt;a href=&quot;https://day.js.org/docs/en/plugin/utc&quot; target=&quot;_blank&quot; rel=&quot;noopener nofollow ugc&quot;&gt;utc&lt;/a&gt;

&lt;pre class=&quot;ql-syntax&quot; spellcheck=&quot;false&quot;&gt;// 上個月第一天 ( 格林威治時間 )
dayjs().utc().subtract(1, &#039;month&#039;).startOf(&#039;month&#039;).toDate();
&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>上個月的起訖 , 我會用 <a href="https://day.js.org/docs/en/manipulate/start-of" target="_blank" rel="noopener nofollow ugc">startOf</a> 跟 <a href="https://day.js.org/docs/en/manipulate/end-of" target="_blank" rel="noopener nofollow ugc">endOf</a></p>
<pre class="ql-syntax" spellcheck="false">// 上個月第一天
 dayjs().subtract(1, 'month').startOf('month').toDate();

// 上個月最後一天
 dayjs().subtract(1, 'month').endOf('month').toDate();
</pre>
<p>如果需要 timezone +0 的上個月起迄 , 我會使用 <a href="https://day.js.org/docs/en/plugin/utc" target="_blank" rel="noopener nofollow ugc">utc</a></p>
<pre class="ql-syntax" spellcheck="false">// 上個月第一天 ( 格林威治時間 )
dayjs().utc().subtract(1, 'month').startOf('month').toDate();
</pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		留言者: Denny		</title>
		<link>https://www.letswrite.tw/dayjs-last-week-month/#comment-213</link>

		<dc:creator><![CDATA[Denny]]></dc:creator>
		<pubDate>Fri, 16 Apr 2021 03:56:43 +0000</pubDate>
		<guid isPermaLink="false">http://localhost:3001/?p=2288#comment-213</guid>

					<description><![CDATA[取得上個月的起訖日期，可以試試這樣寫法 (但我沒做過完整測試...)

使用 day.js

&lt;pre class=&quot;ql-syntax&quot; spellcheck=&quot;false&quot;&gt;let today = dayjs(); // 當日
// let today = dayjs(new Date(2021, 2, 15)); // 若要測試 3 月份的前一月份

// 上月第一天
console.log(
 &#160;today.date(1).subtract(1, &#039;month&#039;).format(&#039;YYYY-MM-DD&#039;)
);
// 或是
console.log(
 &#160;today.date(1).subtract(1, &#039;month&#039;).hour(0).minute(0).second(0).format()
);


// 上月最後一天
console.log(
&#160; today.date(0).format(&#039;YYYY-MM-DD&#039;)
);

// 或是
console.log(
  today.date(0).hour(0).minute(0).second(0).format()
);
&lt;/pre&gt;

「不」使用依賴的話

&lt;pre class=&quot;ql-syntax&quot; spellcheck=&quot;false&quot;&gt;let now = new Date(); // 當日
// let now = new Date(2021, 2, 15); // 若要測試 3 月份的前一月份

// 上月第一天
console.log(
&#160; new Date(now.getFullYear(), now.getMonth() - 1, 1).toLocaleDateString()
);

// 上月最後一天
console.log(
 &#160;new Date(now.getFullYear(), now.getMonth(), 0).toLocaleDateString()
);
&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>取得上個月的起訖日期，可以試試這樣寫法 (但我沒做過完整測試&#8230;)</p>
<p>使用 day.js</p>
<pre class="ql-syntax" spellcheck="false">let today = dayjs(); // 當日
// let today = dayjs(new Date(2021, 2, 15)); // 若要測試 3 月份的前一月份

// 上月第一天
console.log(
 &nbsp;today.date(1).subtract(1, 'month').format('YYYY-MM-DD')
);
// 或是
console.log(
 &nbsp;today.date(1).subtract(1, 'month').hour(0).minute(0).second(0).format()
);


// 上月最後一天
console.log(
&nbsp; today.date(0).format('YYYY-MM-DD')
);

// 或是
console.log(
  today.date(0).hour(0).minute(0).second(0).format()
);
</pre>
<p>「不」使用依賴的話</p>
<pre class="ql-syntax" spellcheck="false">let now = new Date(); // 當日
// let now = new Date(2021, 2, 15); // 若要測試 3 月份的前一月份

// 上月第一天
console.log(
&nbsp; new Date(now.getFullYear(), now.getMonth() - 1, 1).toLocaleDateString()
);

// 上月最後一天
console.log(
 &nbsp;new Date(now.getFullYear(), now.getMonth(), 0).toLocaleDateString()
);
</pre>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
