[解決済み] Javascriptで2桁の年号を取得する方法は?重複
2022-12-03 12:33:40
質問
私はこのフォーマットで現在の日付を書き込むいくつかのjavascriptコードを見つけようとしています:mmddyy
私が見つけたすべてのものは、4桁の年を使用しており、私は2桁を必要としています。
どのように解決するのですか?
この質問に対する具体的な回答は、以下の1行に書かれています。
//pull the last two digits of the year
//logs to console
//creates a new date object (has the current date and time by default)
//gets the full year from the date object (currently 2017)
//converts the variable to a string
//gets the substring backwards by 2 characters (last two characters)
console.log(new Date().getFullYear().toString().substr(-2));
書式設定 フル日付時刻の例、単一関数(MMddyy)。 jsFiddle
JavaScriptを使用します。
//A function for formatting a date to MMddyy
function formatDate(d)
{
//get the month
var month = d.getMonth();
//get the day
//convert day to string
var day = d.getDate().toString();
//get the year
var year = d.getFullYear();
//pull the last two digits of the year
year = year.toString().substr(-2);
//increment month by 1 since it is 0 indexed
//converts month to a string
month = (month + 1).toString();
//if month is 1-9 pad right with a 0 for two digits
if (month.length === 1)
{
month = "0" + month;
}
//if day is between 1-9 pad right with a 0 for two digits
if (day.length === 1)
{
day = "0" + day;
}
//return the string "MMddyy"
return month + day + year;
}
var d = new Date();
console.log(formatDate(d));
書式設定 完全な日付の例、複数の関数(MMddyy)。
// function getMonth with 1 parameter expecting date
// This function returns a string of type MM (example: 05 = May)
function getMonth(d) {
//get the month
var month = d.getMonth();
//increment month by 1 since it is 0 indexed
//converts month to a string
month = (month + 1).toString();
//if month is 1-9 pad right with a 0 for two digits
if (month.length === 1)
{
month = "0" + month;
}
return month;
}
// function getDay with 1 parameter expecting date
// This function returns a string of type dd (example: 09 = The 9th day of the month)
function getDay(d) {
//get the day
//convert day to string
var day = d.getDate().toString();
//if day is between 1-9 pad right with a 0 for two digits
if (day.length === 1)
{
day = "0" + day;
}
return day;
}
// function getYear with 1 parameter expecting date
// This function returns the year in format yy (example: 21 = 2021)
function getYear(d) {
//get the year
var year = d.getFullYear();
//pull the last two digits of the year
year = year.toString().substr(-2);
return year;
}
//A function for formatting a date to MMddyy
function formatDate(d)
{
//return the string "MMddyy"
return getMonth(d) + getDay(d) + getYear(d);
}
var d = new Date();
console.log(formatDate(d));
関連
-
[解決済み] 配列から特定の項目を削除するにはどうすればよいですか?
-
[解決済み] JavaScriptで "use strict "は何をするのか、その根拠は?
-
[解決済み] JavaScriptで文字列が部分文字列を含むかどうかを確認する方法は?
-
[解決済み] あるJavaScriptファイルを他のJavaScriptファイルにインクルードするにはどうすればよいですか?
-
[解決済み] JavaScriptでタイムスタンプを取得する方法は?
-
[解決済み] JavaScriptのオブジェクトをループスルーまたは列挙するにはどうすればよいですか?
-
[解決済み] JavaScriptで文字列をbooleanに変換するにはどうしたらいいですか?
-
[解決済み] JavaScriptで日付の書式設定に関するドキュメントはどこにありますか?
-
[解決済み] ジェスト あるクラスの特定のメソッドをモックする方法
-
[解決済み] JavaScript のオブジェクトの配列を比較し、最小値/最大値を取得する
最新
-
nginxです。[emerg] 0.0.0.0:80 への bind() に失敗しました (98: アドレスは既に使用中です)
-
htmlページでギリシャ文字を使うには
-
ピュアhtml+cssでの要素読み込み効果
-
純粋なhtml + cssで五輪を実現するサンプルコード
-
ナビゲーションバー・ドロップダウンメニューのHTML+CSSサンプルコード
-
タイピング効果を実現するピュアhtml+css
-
htmlの選択ボックスのプレースホルダー作成に関する質問
-
html css3 伸縮しない 画像表示効果
-
トップナビゲーションバーメニュー作成用HTML+CSS
-
html+css 実装 サイバーパンク風ボタン
おすすめ
-
[解決済み] JavaScriptで日付の書式設定に関するドキュメントはどこにありますか?
-
[解決済み] JavaScriptでの大文字小文字を区別しない正規表現
-
[解決済み] javascript includes() 大文字小文字を区別しない
-
[解決済み] ECMAScriptとは?
-
[解決済み] JavaScriptでjson-objectのキーを取得する [重複].
-
[解決済み] JavaScriptで長い配列を小さい配列に分割する方法
-
[解決済み] javascriptでオプションのパラメータを扱う
-
[解決済み] JavaScript で `throw` の後に `return` をする必要がありますか?
-
[解決済み] 変異を伴わないオブジェクトからの値の削除
-
[解決済み] Fetch: ステータスがOKでない場合、プロミスを拒否し、エラーをキャッチするか?