javascript - 在 Firebase 中存储日期

标签 javascript firebase ionic-framework

我正在使用 Ionic 开发一个应用程序,我需要将日期存储在我的 firebase 中。但它变得不正常了,我尝试了像 orderByChild(); 这样的方法但没有任何作用。

我需要像这样存储我的 firebase:

  • 2018 年 3 月 1 日
  • 2018 年 3 月 5 日
  • 2018 年 3 月 10 日

而是这样保存:

  • 2018 年 3 月 1 日
  • 2018 年 3 月 10 日
  • 2018 年 3 月 5 日

无论如何,我可以把它按顺序排列吗?

这是我约会的地方:

function getCurrentDate(){
    var today = new Date();
    console.log(today.getMonth());
    var dd = today.getDate();
    var mm = today.getMonth(); //January is 0!
    var month = new Array();
    month[0] = "Jan";
    month[1] = "Fev";
    month[2] = "Mar";
    month[3] = "Abr";
    month[4] = "Mai";
    month[5] = "Jun";
    month[6] = "Jul";
    month[7] = "Ago";
    month[8] = "Set";
    month[9] = "Out";
    month[10] = "Nov";
    month[11] = "Dez";
    var yyyy = today.getFullYear();

    var currentDate = dd+" "+month[mm]+' '+yyyy;

    return currentDate;
}

这是我进入 firebase 的地方:

function AddServico(date,finalValue,id){
  var deffered = $q.defer();
  var ref = fb.child('/Barbeiro/'+id+'/Dia/'+date+'/');
  ref.once("value")
      .then(function(snapshot) {
          ref.push().set({'finalValue':finalValue});
          deffered.resolve(snapshot);
        });
      ionicToast.show('Adicionado', 'middle', false, 1500);
  return deffered.promise;
}

这是我从 firebase 中读到的:

function GetDias(id){
    var query = fb.child('Barbeiro/'+id+'/Dia/');
    return $firebaseArray(query).$loaded();
}

这是我的 firebase 保持它的方式:

enter image description here

这是我的应用程序的显示方式:

enter image description here

最佳答案

您将日期存储为字符串,不建议这样做。字符串按 lexicographical order 排序,这意味着在执行排序时会相互比较各个字符。这与在字典中对单词进行排序的方式相同。

因此,对于这两个字符串:

  • 2018 年 3 月 10 日
  • 2018 年 3 月 5 日

第一个字符串“小于”第二个字符串,因为字符“1”的 ascii 值小于“5”。

如果你想在数据库中存储日期,我建议使用数字表示而不是字符串,通常是自 UNIX 纪元以来的毫秒数。这些数字会自然地按照您想要的方式按时间顺序排序。

关于javascript - 在 Firebase 中存储日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48855116/

相关文章:

javascript - 不确定这是 Chrome 浏览器上的错误

ios - 如何将整个对象发送到 Firebase 数据库?

javascript - 套接字IO |如何在服务器端获取客户端传输类型?

javascript - MooTools Class.extend 有困难

javascript - 下拉结构与表单数据处理之间的关系

javascript - 如何实现通知计数器?

ios - 如何将 content_available 添加到 Firebase Notification Composer

php - 将 AngularJs 连接到本地服务器 php MySQL

angular - native ionic 网络状态不适用于我的代码

javascript - 如何在 Ionic/Angular 中访问 json 对象