Skip to content

Instantly share code, notes, and snippets.

@sufiiiyan
Last active July 31, 2022 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sufiiiyan/71cb85cdbdeef6268ee5175e8b9eedf6 to your computer and use it in GitHub Desktop.
Save sufiiiyan/71cb85cdbdeef6268ee5175e8b9eedf6 to your computer and use it in GitHub Desktop.
date timezone issue angular support time of client local time... store date in database as utc assumed

This date filter does not, however, convert the date to your browser locale or timezone. Further, the timezone is not presented as a string so that’s not presentation friendly.

Assuming you are following the practice of storing dates in UTC format, you’ll likely want to present dates to the user in their own timezone. To achieve this you can use these javascript Date functions:

toLocaleString() toLocaleDateString() toLocaleTimeString()

An example of this in code:

var convertedDate = dateToConvert.toLocaleString();

The result of toLocaleString() is something like:

"August 30, 2014 at 5:31:41 PM PDT".

Note that it converted the string value to my timezone and provided a string representation of the timezone. Thats what we want, ideally. To let the built-in functions take care of locale and formatting for us. There is, however, still a minor issue. This value will present differently depending on the browser’s default presentation format for date values. For example, toLocaleString() yields a these results on Chrome vs. Firefox:

Chrome: "8/30/2014 5:31:41 PM" Firefox: "August 30, 2014 at 5:31:41 PM PDT"

So, if you want predictable results in the formatting, rather than taking the output of toLocaleString() you’ll want to explicitly define the format string, AFTER you convert to the correct locale.

...convert the date in javascript var convertedDateString = dateToConvert.toLocaleString(); var convertedDate = new Date(convertedDateString); ... later, in angular {{convertedDate | date:'medium'}}

But, the above code unfortunately does not work. Someone had the bright idea that toLocaleString() should include the word “at” in the string sentence.

"August 30, 2014 at 5:31:41 PM PDT"

This produces an “Invalid Date” result when you try to convert it back. Not ISO friendly! I haven’t found a better solution for this except to strip the word “at” from the string and then convert. But, there are probably better javascript libraries than using the built-in capabilities I’m using for this illutration. What I came up with is:

var convertedDateString = dateToConvert.toLocaleString(); convertedDateString = convertedDateString.replace('at ', ''); var convertedDate = new Date(convertedDateString);

And then, this is formatted with a filter:

<p>Date Value: {{convertedDate | date:"medium"}}</p>

In html file, the below:

{{ value | date:'yyyy-MM-dd hh:mm:ss a':'UTC'+ offset}}

In ts file add the following,

offset:number; this.offset = (new Date().getTimezoneOffset());

Get dates in between

getDates(data: any, defaultPrice = 0) {
    let currentDate = moment(); //moment(startBound);
    let endDate = moment().add(90, 'd'); //moment(endBound);
    let days = endDate.diff(currentDate, 'd', false);
    let newData: any = [];
    let valPrice;
    for (let i = 0; i <= days; i++) {
      valPrice = defaultPrice;
      for (let j = 0; j < data.length; j++) {
        if (moment(currentDate).isBetween(moment(data[j].startDate), moment(data[j].endDate), 'days', '[]')) {
          valPrice = data[j].price;
        }
      }
      newData[i] = {
        "start": currentDate.format('YYYY-MM-DD'),
        "end": currentDate.format('YYYY-MM-DD'),
        "title": `$${valPrice}`,
        "backgroundColor": '#000',
        "borderColor": '#000'
      };
      currentDate.add(1, 'd');
    }
    setTimeout(() => {
      this.calendarOptions.events = newData;
    }, 1000);
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment