I use firebase functions . I want to get date after 30 day with MM-DD-YYYY format. I try import datepipe but ı get some error like this : Detailed stack trace: Error: Cannot find module '@angular/common'.
index.ts:
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
admin.initializeApp();
exports.scheduledFunction =
functions.pubsub.schedule("every 5 minutes").onRun((context) => {
const a = new Date();
const targetDate = new Date().setDate(a.getDate() 30);
console.log("Target Date" targetDate);
admin.database().ref("reservations/").update({zafer: "zafer5353"});
return null;
});
Console.log(targetDate) output is :1648669741663 How can ı convert it to MM-DD-YYYY
package.json in functions:
{
"name": "functions",
"scripts": {
"lint": "eslint --ext .js,.ts .",
"build": "tsc",
"serve": "npm run build && firebase emulators:start --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "16"
},
"main": "lib/index.js",
"dependencies": {
"@angular/common": "^13.2.4",
"@angular/fire": "^7.2.1",
"firebase": "^9.6.7",
"firebase-admin": "^9.8.0",
"firebase-functions": "^3.14.1"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^3.9.1",
"@typescript-eslint/parser": "^3.8.0",
"eslint": "^7.6.0",
"eslint-config-google": "^0.14.0",
"eslint-plugin-import": "^2.22.0",
"firebase-functions-test": "^0.2.0",
"typescript": "^3.8.0"
},
"private": true
}
CodePudding user response:
You can use toLocaleDateString()
for your use-case.
Here are the options:
// formats below assume the local time zone of the locale;
// America/Los_Angeles for the US
// US English uses month-day-year order
console.log(date.toLocaleDateString('en-US'));
// → "12/20/2012"
// British English uses day-month-year order
console.log(date.toLocaleDateString('en-GB'));
// → "20/12/2012"
// Korean uses year-month-day order
console.log(date.toLocaleDateString('ko-KR'));
// → "2012. 12. 20."
// Event for Persian, It's hard to manually convert date to Solar Hijri
console.log(date.toLocaleDateString('fa-IR'));
// → "۱۳۹۱/۹/۳۰"
// Arabic in most Arabic speaking countries uses real Arabic digits
console.log(date.toLocaleDateString('ar-EG'));
// → "٢٠/١٢/٢٠١٢"
// for Japanese, applications may want to use the Japanese calendar,
// where 2012 was the year 24 of the Heisei era
console.log(date.toLocaleDateString('ja-JP-u-ca-japanese'));
// → "24/12/20"
// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
console.log(date.toLocaleDateString(['ban', 'id']));
// → "20/12/2012"
For reference, here's the full code:
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
admin.initializeApp();
exports.scheduledFunction =
functions.pubsub.schedule("every 5 minutes").onRun((context) => {
const a = new Date();
const targetDate = new Date().setDate(a.getDate() 30);
const targetDateFormat = new Date(targetDate).toLocaleDateString("en-US", {
year: "numeric",
month: "2-digit",
day: "2-digit"
});
console.log("Target Date: " targetDateFormat);
admin.database().ref("reservations/").update({zafer: "zafer5353"});
return null;
});
You can also specify the year, month and date format. You may also refer here for more information.
You could also use date
objects. We can call any of the getters listed here to get that value and build out the desired string:
const curr_date = ("0" new Date(targetDate).getDate()).slice(-2);
//We add 1 because Jan is indexed at 0 instead of 1
const curr_month = ("0" (new Date(targetDate).getMonth() 1)).slice(-2);
const curr_year = new Date(targetDate).getFullYear();
// Outputs 03-31-2022
console.log(curr_month "-" curr_date "-" curr_year);