This question already has answers here:
Convert date to another timezone in JavaScript
(34 answers)
Closed 8 years ago.
I am trying to convert Local Date/Time to other Time Zone with JavaScript.
Dates are stored in DB as UTC.
For example,
value = "2014-08-15T11:09:10Z"
var dt = new Date(value)
the output will be in my Local Timezone
Fri Aug 15 2014 18:09:10 GMT+0700 (ICT)
But how can I convert this to other Time Zone (i.e - Moscow) by using JavaScript.
You may try like this:
function myTimeZOne(value, zone) {
var f = 'YYYY/MM/DD HH:mm:ss ZZ';
return moment(value, f).tz(zone).format(f);
}
Also check moment.js
You can use JS library such as timezone-js. You could write code like this :
var format = 'YYYY/MM/DD HH:mm:ss ZZ';
var dt = new timezoneJS.Date(format , 'Europe/London');
dt.setTimezone("Asia/Jakarta");
you can also check out other JS library like:
MomentJS
DateJS
Related
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 1 year ago.
I have a date and time in ISO
"time": "2021-10-28T17:30:00.000Z"
Below is what I am doing right now to extract the date and time:
var dateExtract = time.substring(0, 10);
var timeExtract = time.match(/\d\d:\d\d/);
But it is giving me the exact date and time written in ISO.
What I want is to extract the date and time in local date and time. I don't know how to do this.
Use the Date instance.
let obj = {"time": "2021-10-28T17:30:00.000Z"}
const dateTime = new Date(obj.time);
const date = dateTime.toLocaleDateString().replace(/\//g, '-');
// '28-10-2021'
const time = dateTime.toLocaleTimeString()
// '18:30:00' since I am in (+1)
Also find a pool of answers in this similar Question
Just create a date object and use toLocaleString or toLocaleDateString or
toLocaleTimeString
const date = new Date("2021-10-28T17:30:00.000Z")
console.log(date.toLocaleString())
console.log(date.toLocaleDateString().replaceAll("/","-"))
console.log(date.toLocaleTimeString())
You are getting dates in a wrong method.
when you are reading the date it should be converted to your locale, so that you will get exact date from UTC date
You can use JS Date() function or moment library
let d = new Date("2021-10-28T17:30:00.000Z")
d.toDateString()
d.toLocaleDateString()
check Date functions to get date, month, year, time , seconds or timestamp
refer How to format a JavaScript date
https://www.w3schools.com/js/js_dates.asp
https://www.w3schools.com/js/js_date_formats.asp
Or you can use the external moment library
https://momentjs.com/
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
How to initialize a JavaScript Date to a particular time zone
(20 answers)
Closed 4 years ago.
I want datetime in custom format as per below.
2019-01-24T05:52:29:420 +0000 GMT+0000
but when I try do it using below syntax I did not work.
var currentdate = new Date();
alert(currentdate);
Any help? Thanks
Look into Moment.js, you can format JS dates with this quite easily.
e.g:
moment().format('MMMM Do YYYY, h:mm:ss a'); // January 24th 2019, 5:13:29 pm
moment().format('dddd'); // Thursday
in your case:
moment(currentdate).format('the format you would like');
https://momentjs.com/
its very simple you just have to do currentdate.toISOString() which will return "2019-01-24T06:19:19.975Z", now its simple just remove last char from string and concat the other string part in it.
var currentdate = new Date().toISOString();
alert(currentdate.substr(0, currentdate.length-1)+' +0000 GMT+0000');
This question already has answers here:
Extract time from moment js object
(4 answers)
Closed 4 years ago.
I have this date and time format 1/1/2000 20:00:00 and would like to only get the time in this form 20:00 PM or 08:00 AM
How can I format it as hh:mm a to my date?
I get it this way:
var momentDate = moment($scope.workshop.hour, 'YYYY-MM-DDTHH:mm:ss');
var jsDate = momentDate.toDate();
jsDate.toLocaleString();
Try format with HH:mm A as an argument. The A argument will display capital AM or PM as appropriate. Here's an example to get the current time into that format.
var date = new Date()
var momentDate = moment(date);
momentDate.format('HH:mm A')
Updated
Updated to match OP's request.
This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
How do I format a date in JavaScript?
(68 answers)
Closed 4 years ago.
From the server I get '2019-01-19T19:11:00.000Z' I need to convert to local timezone, so that I end up with '2019-01-19T11:11:00'. My UTC offset is 8 hrs.
new Date('2019-01-19T19:11:00.000Z') produces Sat Jan 19 2019 11:11:00 GMT-0800 (Pacific Standard Time), how do I get it back to '2019-01-19T11:11:00'? Thanks
You want the date string in iso format, respecting the local time zone:
const tzoffset = (new Date()).getTimezoneOffset() * 60000;
const d = new Date('2019-01-19T19:11:00.000Z')
console.log(new Date(d - tzoffset).toISOString().split('.')[0])
console.log('2019-01-19T11:11:00')
var now = new Date();
console.log(now.toISOString().split('.')[0]);
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert a Unix timestamp to time in Javascript
I am getting date in the form of unixtimestamp
ex: 1321367459.0 (Equivalent to Tue, 15 Nov 2011 14:30:59)
I want to convert the time stamp to date format using javascript
using jquery also no problem
var myDate = new Date(1321367459.0 * 1000);
You can get a Date object using : var d = new Date(milliseconds);.