Displaying Different Time-Zone in Single HTML Page - javascript

I want do display Dates from different time-zone (PDT, IST, JST and AEST) on single HTML page. I'm not very proficient in JavaScript learning currently. Any help is really appreciated.

In case you want to display current date, the javascript Date() will automatically get the date based on local timezone.
var date = new Date();
You can also refer to all the methods javascript date supports here -> https://www.w3schools.com/jsref/jsref_obj_date.asp
Best way to handle dates is by using moment library
https://momentjs.com/timezone/

Related

How to save the current date/time when I add new value to Firebase Realtime Database in JavaScript

I want to add data in firebase database using webpage.
For unique id I want to use date and time like below format
MTB + yyyyddmmhhmmss
MTB20190517083578
How to do this? Please tell me any one.
I strongly recommend you to use moment.js wich handles a lot of weird Date behaviours (like non UTC dates and formatting).
If you use moment you can format the date using:
moment().format('MTBYYYYMMDDhhmmss')

Date and time without timestamp in Javascript

In Javascript I need to work with the concepts of date, time and "date and time " without referring to a particular point in time. This is exactly the same semantics that joda time's LocalDate and LocalTime provide in Java. I've been briefly looking at Date.js and moment.js, but both libraries seem to build on the Date object, which represents a point in time. Is there any javascript library that provides what I need?
Use case:
There is a model entity -a coupon- which has an expiration date (joda time's LocalDate). I want to compare that date with today's date, so I need a representation of today's date (actually a string in yyyy-mm-dd format would do). I know that today's date, and hence the result of the comparison too, will depend on the timezone settings of the browser's but that's not a problem.
I've started a few times on a JavaScript library with similar API to Noda Time / Joda Time / Java 8. I definitely see value in that. However, there's nothing out there as of yet, as far as I know. There are other reasons that make the Date object less than ideal. I'll try to remember to update this post when/if I ever get a new library off the ground, or if I learn of one created by someone else.
In the mean time, the easiest thing would be to use moment.js:
var expDateString = "2015-06-30";
var exp = moment(expDateString, "YYYY-MM-DD");
var now = moment();
if (exp.isAfter(now))
// expired
else
// valid
You could also do this with plain JavaScript, but there are some gotchas with parsing behavior. Moment is easier.

How to convert a date which is in user timezone to JST or any other timezone using Date() in JavaScript?

I want to convert a date which i get from new Date() to JST timezone or any other timezone without using any JavaScript library. The method should be generalized so that in future if i want to switch to any other timezone, it should be able to do so.
Timezones are not that straight forward (some locations do not follow the rules), so you will never get that accurate without a lib which includes a DB of locations e.g., https://github.com/mde/timezone-js
However, I believe you're looking for getTimezoneOffset()

Date format on client browser

I am getting a date from the server as a javascript string(GMT) or unix timestamp(GMT).
I can convert it to a javascript date object with
var date = new Date(string) or var date = new Date(string)
This gives me the date variable with proper system time-zone's time.
Currently I am displaying this- date.toLocaleString()
Which gives me a nicely formatted date/time according to my system locale and time zone.
I am using this to automatically accomodate for DST if the client browser follows it.
Previously I was required to display this date only in EST, but when the US time started following EDT, I was told to display it in EST. I think this approach would simplify displaying the time/date acoording to user's system time setting.
Is there any disadvantage or possible bug accociated with this approach?
If yes, what would be the best way to display this date in the browser, so that it displays correctly according to the timezone(accomodating DST if any) in which the user(cient browser) is?
As pointed out in the comments, the output may be different depending on the users settings. A more reliable and flexibe solution is moment.js, which is a great library.
moment("2013-04-04", "YYYY-MM-DD").format("MMM Do YY"); //"Apr 4th 13"

How to validate date in javascript

I have a SharePoint Date Picker Control which is used to select a BirthDate . I want the birthdate to be atleast 16years older from the current date.
Can anyone guide me how to validate this using javascript? I have not used javascript much, Please guide me how to go about the validation process?
Look at using the library date.js:-
http://www.datejs.com/
This library adds a number of methods for working with date objects.
Without using an external library, the easiest way to compare a date of birth is over 16 is to convert the date to an epoch timestamp and then compare this against a timestamp of now minus 16 years.
Be aware that while most languages/platforms use timestamps in seconds, Javascript uses milliseconds, so when comparing against timestamps from external sources, you need to divide by a factor of 1000.

Categories

Resources