如何以YYMMDD格式将日期传递到firebase?

时间:2017-10-28 05:15:39

标签: angularjs firebase firebase-realtime-database

我在angularJS

中创建了一个相册功能

$scope.createAlbumDialog = function(event, productObj) {
    //HERE WE WILL SET THE userCategoryKeyId FROM THE productObj
    $scope.selectedCategoryKey  =  productObj.userCategoryKeyId;
    $scope.albumName            =  $scope.user.userSearchInProduct;

    //HERE WE WILL CREATE FB REF. AND CHECK ALBUM ALREADY EXIST OR NOT
    if($scope.selectedProductKey.length > 0) {
      var PRODUCT_DB_REF = firebase.database().ref('datastore/productsAlbum');
					
      //HERE WE WILL PASS THE KEYID INTO THE FIREBASE NODE'S 
      PRODUCT_DB_REF.child($scope.selectedCategoryKey).child(AUTH.userTypeKeyId).
      orderByChild("albumNameLC").equalTo($scope.albumName.toLowerCase()).once('value').then(function (snapshot) {
         var value = snapshot.val();
          if (value) {
            //SHOW ALERT IF PRODUCT IS ALREADY EXIST IN THE ALBUM
            CMN.showNotification("bottom","right","info", "Album is already exist");

          } else {
            //HERE WE WILL GET THE TIME DURING PUSH THE DATA INTO THE FIREBASE....
            var date = new Date().getTime();

            PRODUCT_DB_REF.child($scope.selectedCategoryKey).child(AUTH.userTypeKeyId).push({
              albumName             : $scope.albumName,
              albumNameLC           : $scope.albumName.toLowerCase(),
              sellerUserTypeKeyId   : Number(AUTH.userTypeKeyId),
              productKey            : $scope.selectedProductKey,
              createDate            : Number(date)
            });
            //SHOW ALERT AFTER CREATED THE ALBUM
            CMN.showNotification("bottom","right","success", "Album is successfully created");
          };
        }
      )
     //HERE WE WILL BLANK THE INPUT FIELD AFTER CLICK ON THE BUTTON
     $scope.user.userSearchInProduct = "";
  } else {
				 	//SHOW ALERT IF PRODUCT IS NOT FOUND
					swal('<span style="color:red"><h4>Select atleast 1 product for create the album</h4><span>'); 
			 	}
			}

并且我必须将createDate传递给firebse并且我使用新的Date()。getTime()和我的日期格式是 - 1509167255906但我必须将firebase传入YYMMDD格式如何执行此操作?

1 个答案:

答案 0 :(得分:1)

你可以这样做

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
   if(dd<10)
      dd='0'+dd;
   if(mm<10)
      mm='0'+mm;

   var formatedDate= yyyy +'/'+mm+'/'+dd; or var formatedDate= yyyy +'-'+mm+'-'+dd; 
   //pass the formatedDate into firebase push

  PRODUCT_DB_REF.child($scope.selectedCategoryKey).child(AUTH.userTypeKeyId).push({
          albumName             : $scope.albumName,
          albumNameLC           : $scope.albumName.toLowerCase(),
          sellerUserTypeKeyId   : Number(AUTH.userTypeKeyId),
          productKey            : $scope.selectedProductKey,
          createDate            : formatedDate,
        });

See this for sample