0

So this is how my code looks like

cropref.child(mycrop.name).push({
                          cropname:mycrop.name,
                          croplocation:mycrop.location,
                          cropplantdate:mycrop.plantdate.toString(),
                          cropharvestdate:mycrop.harvestdate.toString(),         
                  })

mycrop.harvestdate and mycrop.plantdate are both date inputs from my html

<input type="date" ng-model='mycrop.harvestdate'>

<input type="date" ng-model='mycrop.harvestdate'>

to be able to put the data on my Firebase database , I need to convert it first into string

cropplantdate:mycrop.plantdate.toString(),      

but the data on my Firebase database includes time and timezone

sample data

Sat Dec 12 2020 00:00:00 GMT+0800 (Malay Peninsula Standard Time)

so once I call the data from my database, I can't filter it since it's not a date anymore but a string. How do I solve this problem so that I can filter date (which is converted to string) stored inside my database

adjuremods
  • 2,938
  • 2
  • 12
  • 17
Nevin
  • 365
  • 5
  • 18
  • If you insist on storing dates as strings, store them in a format that is chronological in lexicographical order. E.g. `2016-11-27T17:01:19`. But better yet: simply store the timestamp as milliseconds since the epoch. See http://stackoverflow.com/questions/34957249/storing-dates-with-angular-and-firebase/34957326#34957326 – Frank van Puffelen Nov 27 '16 at 16:01

2 Answers2

3

Two options

1) Store the date in a more generic, but human readable format, so right now would be Sunday November 27 at 09:13:38

20161127091338

2) Store the date as a unix timestamp (in milliseconds) using

(new Date).getTime() / 1000

There are a lot of variants to #2 so do some research to see which is best for your use case.

You can save either answer as a string but #1 would be more easily searchable since queries wouldn't require any kind of conversions - to see todays events

queryStartingAt("20161127") and queryEndingAt("20161127")
Jay
  • 34,438
  • 18
  • 52
  • 81
  • i like the number one but how do i convert it back to date format sir ? like mm/dd/yyyy again – Nevin Nov 27 '16 at 14:32
  • @Nevin There are a bunch of ways to convert dates but check this question and answer out [Angular Date Converstion](http://stackoverflow.com/questions/28493843/convert-date-formats-using-angularjs) – Jay Nov 28 '16 at 18:04
0

You need to convert the date first in your Format. You can use SimpleDateFormatFormat for that.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

Now you can easily format your date to this format

String mynewdate = sdf.format(mycrop.plantdate.getTime());

The Output for today would be:

2016-11-27

Of course you can reverse that back to a Calendar. I do it this way:

 public static Calendar fromStringtoCalendar(String datestring){
    int year =  Integer.valueOf(datestring.substring(0, 4));
    int month =  Integer.valueOf(datestring.substring(5, 7)) -1;
    int day =  Integer.valueOf(datestring.substring(8, 10));
    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month, day);
    return calendar;
}
Hhorn
  • 223
  • 2
  • 6