Cursorの日付取得を正確にできない問題を解決する

に公開

Cursorの日付取得問題を5分で解決してみる。

Cursorでドキュメント作成やファイル生成を行う際、日付がズレたり古い日付が使われることがありませんか?

  • この記事では、Cursorが正確な現在時刻を取得できない問題を、User Rulesを使って根本的に解決する方法をご紹介します。

問題の概要

Cursorでファイルを作成する際に以下のような問題が発生することがあります:

  • 古い日付が使われる:昨日や数日前の日付でファイルが作成される
  • 時刻情報が不正確:タイムゾーンがずれている
  • ファイル名の日付ミス2025-07-23.mdのようなファイル名で今日が2025-07-25の場合

これは、CursorのAIシステムが起動時に正確な現在時刻を把握できていないことが原因です。

解決方法:User Rulesで自動時刻取得

手順1:必要なスクリプトを準備

まず、プロジェクトに以下のファイル構造を作成します:

your-project/
├── scripts/
│   └── getDate.js
└── system/
    └── utils/
        └── date-utils.js

system/utils/date-utils.js

/**
 * 現在の日付を取得してフォーマットする
 * @returns {string} YYYY-MM-DD形式の日付文字列
 */
function getCurrentDate() {
    const now = new Date();
    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, '0');
    const day = String(now.getDate()).padStart(2, '0');
  
    return `${year}-${month}-${day}`;
}

/**
 * 現在の日時を取得してフォーマットする
 * @returns {string} YYYY-MM-DD HH:MM:SS形式の日時文字列
 */
function getCurrentDateTime() {
    const now = new Date();
    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, '0');
    const day = String(now.getDate()).padStart(2, '0');
    const hours = String(now.getHours()).padStart(2, '0');
    const minutes = String(now.getMinutes()).padStart(2, '0');
    const seconds = String(now.getSeconds()).padStart(2, '0');
  
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

module.exports = {
    getCurrentDate,
    getCurrentDateTime
};

scripts/getDate.js

#!/usr/bin/env node

// 日付ユーティリティ関数をインポート
const { getCurrentDate, getCurrentDateTime } = require('../system/utils/date-utils');

/**
 * 現在の日付・時刻情報を取得して表示
 */
function getSystemDateTime() {
    const currentDate = getCurrentDate();
    const currentDateTime = getCurrentDateTime();
  
    console.log('🕐 システム時刻情報');
    console.log('========================');
    console.log(`📅 日付: ${currentDate}`);
    console.log(`⏰ 時刻: ${currentDateTime}`);
    console.log('========================');
  
    return {
        date: currentDate,
        datetime: currentDateTime,
        timestamp: Date.now()
    };
}

// メイン実行
if (require.main === module) {
    try {
        const dateInfo = getSystemDateTime();
      
        // 環境変数として設定
        process.env.SYSTEM_DATE = dateInfo.date;
        process.env.SYSTEM_DATETIME = dateInfo.datetime;
      
        console.log('✅ システム時刻情報を正常に取得しました');
      
    } catch (error) {
        console.error('❌ システム時刻情報の取得に失敗しました:', error.message);
        process.exit(1);
    }
}

module.exports = { getSystemDateTime };

手順2:Cursor SettingsでUser Rulesを設定

  1. Cursor Settingsを開く

    • Cmd + , (Mac) または Ctrl + , (Windows/Linux)
  2. User Rulesを追加

    • 左メニューから「Rules & Memories」を選択
    • 「User Rules」セクションで「+ Add Rule」をクリック
  3. ルールを入力

    Always run $0_scripts/getDate.js to know what time it is at first.
    

手順3:動作確認

設定後、Cursorを再起動すると、以下のように正確な時刻が表示されます:

🕐 システム時刻情報
========================
📅 日付: 2025-07-24
⏰ 時刻: 2025-07-24 14:10:13
========================
✅ システム時刻情報を正常に取得しました
🌍 タイムゾーン: Asia/Tokyo
🌐 ロケール: ja-JP

効果とメリット

▫️解決される問題

  • 正確な日付でファイル作成:常に今日の日付でファイルが作成される
  • タイムゾーン問題解消:ローカルのタイムゾーンが正しく反映
  • 一貫性のある時刻情報:プロジェクト全体で統一された時刻情報

▫️追加メリット

  • 自動実行:Cursor起動時に自動で時刻を取得
  • 環境変数設定:他のスクリプトからも時刻情報を参照可能
  • エラーハンドリング:時刻取得に失敗した場合の適切な処理

まとめ

この解決方法により、Cursorでの日付関連の問題は完全に解消されます。特に以下のような作業で効果を実感できるでしょう:

  • 議事録作成:正確な日付でファイル生成
  • ブログ記事:投稿日が正しく設定される
  • ファイル管理:日付ベースのファイル名が正確

5分程度の簡単な設定で、日々の開発効率が大幅に向上します。ぜひお試しください!

参考リンク

Discussion