在Firebase中存储日期 [英] Storing date in Firebase

查看:72
本文介绍了在Firebase中存储日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Ionic做一个应用程序,需要将日期存储在我的Firebase中.但是它变得混乱了,我尝试了诸如orderByChild();这样的方法.但没什么.

I'm doing an app with Ionic and I need to store date in my firebase. But it's getting out of order, i tried methodos like orderByChild(); but nothind is working.

我需要我的Firebase这样存储:

I need my firebase to store like this:

  • 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;
}

这是我进入火力场的地方:

Here's where I push into 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上读取它的地方:

And here is where I read it from firebase:

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

这是我的火力库保持火力的方式:

Here is how my firebase keep it:

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

Here's how my app shows it :

推荐答案

您将日期存储为字符串,不建议使用.字符串按词典顺序进行排序,这意味着在执行过程中各个字符会相互比较有点.这与您对字典中的单词进行排序的方式相同.

You're storing dates as string, which is not recommended. Strings are sorted in lexicographical order, which means that the individual characters are compared to each other when performing a sort. This is the same way you'd sort words in a dictionary.

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

So, for these two strings:

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

第一个字符串小于"第二个字符串,因为字符"1"的ascii值小于"5".

The first string is "less than" the second, because the ascii value of character "1" is less than "5".

如果要将日期存储在数据库中,建议使用数字表示形式而不是字符串,通常是自unix纪元以来的毫秒数.这些数字自然会按照您希望的时间顺序进行排序.

If you want to store dates in the database, I'd suggest using a numeric representation instead of a string, usually the number of milliseconds since the unix epoch. These numbers will naturally sort chronologically the way you want.

这篇关于在Firebase中存储日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆