[javascript] Display date/time in user's locale format and time offset

_x000D_
_x000D_
// new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]])_x000D_
var serverDate = new Date(2018, 5, 30, 19, 13, 15); // just any date that comes from server_x000D_
var serverDateStr = serverDate.toLocaleString("en-US", {_x000D_
  year: 'numeric',_x000D_
  month: 'numeric',_x000D_
  day: 'numeric',_x000D_
  hour: 'numeric',_x000D_
  minute: 'numeric',_x000D_
  second: 'numeric'_x000D_
})_x000D_
var userDate = new Date(serverDateStr + " UTC");_x000D_
var locale = window.navigator.userLanguage || window.navigator.language;_x000D_
_x000D_
var clientDateStr = userDate.toLocaleString(locale, {_x000D_
  year: 'numeric',_x000D_
  month: 'numeric',_x000D_
  day: 'numeric'_x000D_
});_x000D_
_x000D_
var clientDateTimeStr = userDate.toLocaleString(locale, {_x000D_
  year: 'numeric',_x000D_
  month: 'numeric',_x000D_
  day: 'numeric',_x000D_
  hour: 'numeric',_x000D_
  minute: 'numeric',_x000D_
  second: 'numeric'_x000D_
});_x000D_
_x000D_
console.log("Server UTC date: " + serverDateStr);_x000D_
console.log("User's local date: " + clientDateStr);_x000D_
console.log("User's local date&time: " + clientDateTimeStr);
_x000D_
_x000D_
_x000D_

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to datetime

Comparing two joda DateTime instances How to format DateTime in Flutter , How to get current time in flutter? How do I convert 2018-04-10T04:00:00.000Z string to DateTime? How to get current local date and time in Kotlin Converting unix time into date-time via excel Convert python datetime to timestamp in milliseconds SQL Server date format yyyymmdd Laravel Carbon subtract days from current date Check if date is a valid one Why is ZoneOffset.UTC != ZoneId.of("UTC")?

Examples related to timezone

How to set the timezone in Django? How to convert Moment.js date to users local timezone? What does this format means T00:00:00.000Z? How do I get the current timezone name in Postgres 9.3? MySQL JDBC Driver 5.1.33 - Time Zone Issue Python get current time in right timezone Symfony2 and date_default_timezone_get() - It is not safe to rely on the system's timezone settings PHP date() with timezone? How to store a datetime in MySQL with timezone info Java SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") gives timezone as IST