Create a Date with a set timezone without using a string representation
using .setUTCHours()
it would be possible to actually set dates in UTC-time, which would allow you to use UTC-times throughout the system.
You cannot set it using UTC in the constructor though, unless you specify a date-string.
Using new Date(Date.UTC(year, month, day, hour, minute, second))
you can create a Date-object from a specific UTC time.
Parse date without timezone javascript
The date is parsed correctly, it's just toString
that converts it to your local timezone:
let s = "2005-07-08T11:22:33+0000";
let d = new Date(Date.parse(s));
// this logs for me
// "Fri Jul 08 2005 13:22:33 GMT+0200 (Central European Summer Time)"
// and something else for you
console.log(d.toString())
// this logs
// Fri, 08 Jul 2005 11:22:33 GMT
// for everyone
console.log(d.toUTCString())
Setting UTC timezone with numeric values in JS [UTC+2, UTC+3, UTC-12, etc]
I tried to use moment.js but...
Didn't you come accross those methods: .utc(), .add() and .format() ?
EDIT
To accomodate non-full hour offsets, you can use .match to retreive the sign (+ or -) of the offset, the hour and the optionnal minute.
Here is the actual possible offsets list: Wikipedia
It outputs UTC time if your offset only is UTC
or is an empty string.
const timeOffsets = ["UTC+2", "UTC+3", "UTC-12", "UTC+5:45", "UTC-3:30", "UTC", ""]
function getlocalTime(offset) {
const time = offset.match(/([\+\-])(\d{1,2}):?(\d{2})?/)
let sign = ""
let numericOffset = 0
if (time) {
sign = time[1]
numericOffset = parseInt(time[2]) + (time[3] ? parseInt(time[3]) / 60 : 0)
}
return moment().utc().add(sign + numericOffset, "hours").format("YYYY-MM-DD HH:mm:ss")
}
const result = timeOffsets.map(getlocalTime)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
Convert Date/Time for given Timezone - java
For me, the simplest way to do that is:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//Here you say to java the initial timezone. This is the secret
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
//Will print in UTC
System.out.println(sdf.format(calendar.getTime()));
//Here you set to your timezone
sdf.setTimeZone(TimeZone.getDefault());
//Will print on your default Timezone
System.out.println(sdf.format(calendar.getTime()));
Related Topics
No Visible Cause For "Unexpected Token Illegal"
Sum JavaScript Object Propertya Values With the Same Object Propertyb in an Array of Objects
Why Does Babel Rewrite Imported Function Call to (0, Fn)(...)
What's the Right Way to Decode a String That Has Special HTML Entities in It
Synchronized Scrolling Using Jquery
Setting Minimum Size Limit for a Window Minimization of Browser
Repeat Animation Every 3 Seconds
Call JavaScript Function from Native Code in Wkwebview
What Is the Standard Solution in JavaScript for Handling Big Numbers (Bignum)
What Is the Significance of the JavaScript Constructor Property
Take a Screenshot of a Webpage With JavaScript
Backslashes - Regular Expression - JavaScript
Display Data Streamed from a Flask View as It Updates
What Is the JavaScript ≫≫≫ Operator and How to Use It
Modify a CSS Rule Object with JavaScript
How to Check If an External (Cross-Domain) CSS File Is Loaded Using JavaScript