I want to validate the date- time format which is like 'Wed Oct 25 2017 12:59:00 GMT+0800 (中国标准时间)' for this i am looking for a regex.
Tried few combinations but those did not work for me.
I would suggest using moment.js instead to parse and validate your datetime string:
var time = 'Wed Oct 25 2017 12:59:00 GMT+0800 (中国标准时间)';
console.log(moment(time, 'ddd, MMM DD YYYY HH:mm:ss ZZ').isValid());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.js"></script>
The format string 'ddd, MMM DD YYYY HH:mm:ss ZZ' tells Moment how to read your GMT+0800 (China Standard Time) input string.
Note: All date objects created with Moment, no matter, the time zone will default to the current time zone, unless specified directly, e.g. .zone("+08:00") To get UTC times back, use moment.utc() instead. For more information on time zones, check out the section Moment Time Zones.
Further reading: Moment.js: A Better Date Library for JavaScript
Related
I'm trying to convert a time in the format of '2019-07-15T08:57:58.749081' to a local time using the format of 'Month Day(th/st) Year Hour:mm am/pm". So anything like "September 9th 2018 9:40 pm" or "July 18th 2019 9:40 pm", etc.
This is to use the moment package imported into a ReactJS app. I can get the format to look right but the time is still GMT/UTC. To do this format I used
var dateTime = moment(param).format('MMMM Do YYYY h:mm a');
But I really need the formatted time in my own local time.
Since the input string is in ISO 8601 format, and the time basis is UTC, the string should contain a trailing Z. In other words, it should look like 2019-07-15T08:57:58.749081Z. Since it doesn't, you have two choices.
You can append the Z yourself:
moment(param + 'Z').format('MMMM Do YYYY h:mm a')
You can parse as UTC and then switch to local mode before formatting:
moment.utc(param).local().format('MMMM Do YYYY h:mm a')
I'm working with a date that looks like this:
Mon Feb 04 2019 15:57:02 GMT-0700 (Mountain Standard Time)
and I'm trying to convert it to this:
2019-02-04T15:57:02.000Z
but for some reason my code always adds 7 hours and ends up being like this:
"2019-02-05T22:57:02.000Z"
Can anyone tell me what I'm doing wrong? Thanks a lot in advance!
Here's my code:
new Date(myTime as string).toISOString();
I'd use Moment.js, which is a decent date parsing and formatting library. To get what you're looking for, you'd use a statement like:
console.log(moment
.parseZone(
"Mon Feb 04 2019 15:57:02 GMT-0700 (Mountain Standard Time)",
"ddd MMM DD YYYY HH:mm:ss 'GMT'ZZ") // the format of the string presented
.local()
.format('YYYY-MM-DDTHH:mm:ss')); // the format of the output
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
I've broken the single line out into parts so it's a bit easier to read. A few notes:
parseZone allows you to parse the "-0700" from the string.
local converts the date from the parsed time zone to the current time zone
format formats the date.
The format topic has a list of the formatting tokens used.
The Parse > String + Format topic lists the parsing tokens (which are the same as the formatting tokens for the most part).
Note that the output does not have a "Z" at the end; this is important because without the "Z", it is a local date. With the "Z" you are are actually specifying a date and time that is 7 hours earlier than the one you've been given.
I'm not sure how to get this as a one-liner, but this is one way:
var time = new Date('Mon Feb 04 2019 15:57:02 GMT-0700 (Mountain Standard Time)')
new Date(time.setHours(time.getHours() + 7)).toISOString()
"2019-02-05T12:57:02.000Z"
Your code is not adding hours to the input date. What is happening is that your date string is using a particular timezone GMT-0700 (Mountain Standard Time) and the time zone used in new Date().toISOString() is UTC GMT+0000 (UTC). So when in the Mountain Standard Time timezone is Mon Feb 04 2019 15:57:02, in the UTC timezone is actually 2019-02-05T22:57:02.000Z. There are your seven hours from GMT-0700 to GMT+0000.
EDITED
If you don't really care about time zones and want to obtain 2019-02-04T15:57:02.000Z from Mon Feb 04 2019 15:57:02 GMT-0700 (Mountain Standard Time) you could just strip everything after GMT to let new Date() think it is an UTC date.
var timeString = 'Mon Feb 04 2019 15:57:02 GMT-0700 (Mountain Standard Time)';
new Date(timeString.substr(0, timeString.indexOf('GMT') + 3));
2019-02-04T15:57:02.000Z
I have an input date with a specific time zone in string. For example:
Sat May 20 2017 17:00:00 GMT-0300 (-03)
I want to change the timezone of this date to my local timezone without converting the time. As if the input date was in my correct timezone.
Sat May 20 2017 17:00:00 GMT+0200 (CEST)
I played around with moment and found this:
const date = moment(moment.utc('Sat May 20 2017 17:00:00 GMT-0300 (-03)').format('LLL'))
Is it the simplest way to do it? I don't like the copy of the moment object but I can't find a method to do it in a "single shot".
To parse your input in local time, you can simply use moment(String, String) function. As docs says:
By default, moment parses and displays in local time.
Here a live sample:
var input = 'Sat May 20 2017 17:00:00 GMT-0300 (-03)';
var m = moment(input, 'ddd MMM DD YYYY HH:mm:ss');
console.log(m.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
I have the following date in utc format which I want to convert and display as local date time.
// utc
date = 'Thu Oct 27 2016 07:00:00 GMT+0100 (GMT Standard Time)';
// convert to local
moment.utc(date).local().format("ddd MMM DD YYYY HH:mm:ss");
Expected results using the above should be:
Thu Oct 27 2016 08:00:00
But actual results are being displayed as follows i.e. the time is not changing?
Thu Oct 27 2016 07:00:00
A few things:
Your input is a string, in a nonstandard format. You probably got it by taking a Date object and calling the .toString() function, but that doesn't mean it's suitable for parsing by moment. If you look in the debug console, you'll find a warning message, because you passed in something moment doesn't understand natively and so it is passing it back to the Date constructor. See the user guide, which explains that you must pass a format specifier when you have custom input.
Your input contains a time zone offset of +0100. That means the 7:00 time is at UTC+1, which is equivalent to 6:00 UTC. This is the value the moment has when you first parse it with moment.utc(yourInput)
You then convert 6:00 UTC back to local time when you call the local() function. That's the 7:00 UTC+1 you are seeing.
How to resolve this depends on what you are actually wanting to do. You said you expected 8:00 UTC, but that doesn't make sense from the input you gave because 7:00 (+1) == 6:00 (+0).
If what you meant is that you wanted to assert that the original time value was indeed UTC, and not UTC+1, even though it's in your input, then provide a format string that only contains the parts you care about. Moment's default loose parsing mode will ignore anything extraneous.
date = 'Thu Oct 27 2016 07:00:00 GMT+0100 (GMT Standard Time)';
moment.utc(date, 'ddd MMM DD YYYY HH:mm:ss').local().format("ddd MMM DD YYYY HH:mm:ss");
In other words, your input date is only Thu Oct 27 2016 07:00:00 here, and the rest is ignored. You are asserting that it's in UTC by passing it in to moment.utc, and then when you convert it to local it will end up at 8:00.
However, though that's what you asked for, my guess is that date is actually a JavaScript Date object, whose string output of .toString() is what you are showing here. In that case, the Date object is already representing a specific point in time, which is 6:00 UTC. So it doesn't matter whether you do moment.utc(date).local() or just moment(date). Both will leave you with that same moment in time, represented with the local time zone.
You may want to go back to your input and figure out how you got it to include GMT+0100 when you are thinking it's actually a UTC value. Consider that if you actually had 8:00 UTC, your input would say 09:00 GMT+0100.
You may want to use Moment TimeZone with MomentJS (http://momentjs.com/timezone/)
date = moment.tz("Thu Oct 27 2016 07:00:00", "GMT Standard Time");
date.clone().local().format('ddd MMM DD YYYY HH:mm:ss');
EDIT:
Removing the utc part fixes it for me without Moment TimeZone:
date = 'Thu Oct 27 2016 07:00:00 GMT+0100 (GMT Standard Time)';
moment(date).local().format('ddd MMM DD YYYY HH:mm:ss');
I'm using moment() and want to know if there's a shortcut to give me the same date string as new Date() would give me.
I have to do some timezone conversions so using moment.js makes that easier, but I need the same date format: Thu Oct 06 2016 23:08:53 GMT-0700 (PDT) as the native Date object would give me.
I would assume there is a shortcut for this, but I can't find it.
moment.tz('America/New_York').format('???')
I could not find a shortcut however this gives the string I need:
moment.tz('America/New_York').format('ddd MMM DD YYYY HH:mm:ss [GMT]ZZ (z)')
from the docs: http://momentjs.com/docs/#/displaying/
Returns: Fri Oct 07 2016 02:25:17 GMT-0400 (EDT)
if you have no idea which timezone the browser is currently in, you can try moment.tz.guess(), it isn't 100% correct for timezone offset that has multiple names though:
var d = moment()._d;
d.toString().replace(/\([\w ]+\)$/, moment.tz(d, moment.tz.guess()).format('(z)'));
The date format you're referring to, depends on the culture/language of your browser. That aside, if you want the same format as the standard new Date().toString() returns, you just use the toString() method on the moment object, without any arguments:
moment().toString();
Source: http://momentjs.com/docs/#/displaying/as-string/