i am using nodejs to connect to mySql and connection works.
while using query such as
SELECT * from someTable
i get the required results. But the problem is that i am having
a column which stores date is having the given format.
before sending the data i need this format to be converted and vice versa
file_date : Wed Jan 01 2014 05:34:53 GMT+0530 (IST)
This is format i am getting
i am unable to send params with Date in this format from client side
so i need to convert this data to "DD-MM-YYYY" format.i need this way so that i can pass params in "DD-MM-YYYY" and fetch data date wise
Simple Approach
If you know your date format is supported in Moment, then the simplest approach is to construct a moment object and format it.
var file_date = 'Wed Jan 01 2014 05:34:53 GMT+0530 (IST)';
var formatted = moment(file_date).format('DD-MM-YYYY');
Deprecation Warning:
However, at some point support for non ISO date formats was deprecated. It will in some cases still work but you should be warned (per the deprecation warning message):
Deprecation warning: value provided is not in a recognized ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non ISO date formats are discouraged and will be removed in an upcoming major release.
Workaround
Instead, if you know the format of an input string, you can use that to parse a moment.
var s = 'Wed Jan 01 2014 05:34:53 GMT+0530 (IST)';
var m = moment(s, 'ddd MMM DD YYYY hh:mm:ss [GMT]ZZ').format('MM-DD-YYYY');
console.log(m);
<script src="https://momentjs.com/downloads/moment.min.js"></script>
When specifying a format manually like this, it helps to have the format documentation handy: https://momentjs.com/docs/#/displaying/format/
Related
I have this format date:
01Mar-0234
26Feb-0430
01 is day, mar or feb is mounth and 0430 is 4 oclock 30 in formater zulu +00.
I would like to use moment for converting this format, I'm trying this:
moment('26Feb-0430').format("DD-MM-YY HH:MM");
but I haven't good format and I have this error :
Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment c
onstruction falls back to js Date(), which is not reliable across all browsers and version
s. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major r
elease. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
Arguments:
Can you help me for have 26/02/2017 06:30 for summer and 5h30 winter?
As suggested in the comments and in the warning message you have to use moment(String, String) parsing function.
In the format string parameter you have to use moment tokens where: DD is day of the month. MMM is month's short name, HH is 0-23 hours and mm (lowercase) is minutes.
Since you have to threat your input as +00:00, you have to use moment.utc.
Use format() to display the parsed moment object passing the tokens you need, always remember that moment tokens are case sensitive.
Here a working sample:
var result = moment.utc('26Feb-0430', 'DDMMM-HH:mm').format("DD/MM/YYYY HH:mm");
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
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/
I use the datepicker to pick a date and send it to the server.
When I log the JS value I get the correct result:
Tue Mar 22 2016 00:00:00 GMT+0100 (Mitteleuropäische Zeit)
but in the ajax request it is
2016-03-21T23:00:00.000Z
I don't modify the values, just giving the object to angulars http function.
Does Angular need some configuration to handle it?
You can try the following piece of code
dateObj.setMinutes((dateObj.getMinutes() + dateObj.getTimezoneOffset()));
No need of localization, use this code just before doing any service call. It will pass you the exact date what you selected in the datepicker.
It will work in all timezone (+) and (-),
Example: 2016-03-21 00:00:00 GMT+0100, the above said code covert it as 2016-03-21 01:00:00 GMT+0000. While on Service it converts it as 2016-03-21 00:00:00.
I think it will solve your problem.
Those two strings represent the same time. One is in UTC, i.e. GMT +0, which you can see from the Z ending. The other is in a different timezone, specifically GMT +1 hour.
If you had javascript date objects for both strings, and turned them into integers, i.e. seconds passed since Jan 1, 1970, UTC, you'd find them identical. They represent the same instant but in two different geographic locations.
var d1 = new Date('Tue Mar 22 2016 00:00:00 GMT+0100');
var d2 = new Date('2016-03-21T23:00:00.000Z');
Number(d1); // 1458601200000
Number(d2); // 1458601200000
Generally this is a good thing. Dealing in timezones gets very confusing. I find it best for a server to always deal in UTC.
https://github.com/angular/material/pull/9410
Check out the 1.1.1+ version. This will solve your issue.
<md-datepicker ng-model="date" ng-model-options="{ timezone: 'utc' }"></md-datepicker>
If suppose am selecting a date like Tue Aug 06 2019 00:00:00 GMT+0530 (India Standard Time), am getting 2019-08-05T18:30:00.000Z. ( which in my case previous date with respect to the selected date)
I made use of toLocalDateString() to do the job.
// this.date = 2019-08-05T18:30:00.000Z
const converted = new Date(this.date).toLocaleDateString();
console.log(converted); // 8/6/2019 (MM/DD/YYYY) format
Given the volume of Timezone questions, I would have thought to be able to find the answer to this issue, but haven't had any success.
Is there a way using moment.js to parse an ISO-8601 string but have it parsed in my local timzeone? Essentially I want to ignore the timezone information that is supplied in the ISO string.
For example, if I am in EDT timezone:
var x = moment( "2012-12-31T00:00:00+0000" );
will give me:
"2012-12-30T19:00:00-5000"
I'm looking to ignore the timezone info and just have it give me a moment equivalent of "2012-12-31T00:00:00-5000" local time (EDT).
I don't think you really want to ignore the offset. That would ultimately just be replacing the offset you provided with one from your local time zone - and that would result in a completely different moment in time.
Perhaps you are just looking for a way to have a moment retain the time zone it was given? If so, then use the moment.parseZone function. For example:
var m = moment.parseZone("2012-12-31T00:00:00+0000");
var s = m.format(); // "2012-12-31T00:00:00+00:00"
You could also achieve this with moment.utc. The difference is that moment.parseZone will retain whatever offset you give it, while moment.utc will adjust to UTC if you give it a non-zero offset.
I solved this by supplying a format as the second argument, and using Moment's method of escaping characters, and wrapped square brackets around the timezone.
moment("2016-01-01T05:00:00-05:00", "YYYY-MM-DDTHH:mm:ss[Z]").startOf("hour").format()
This will still create moment objects using your local time zone, but it won't do any sort of auto-timezone calculation. So the above example will give you 5am regardless of timezone supplied.
I know I'm late to the party, I had the same question and my searches didn't bring me any closer. I broke down and read the documentation and there is an option in moment for a String + Format:
String + Format docs
moment(String, String);
moment(String, String, String);
moment(String, String, Boolean);
moment(String, String, String, Boolean);
and more words, then this:
Unless you specify a time zone offset, parsing a string will create a date in the current time zone.
moment("2010-10-20 4:30", "YYYY-MM-DD HH:mm"); // parsed as 4:30 local time
moment("2010-10-20 4:30 +0000", "YYYY-MM-DD HH:mm Z"); // parsed as 4:30 UTC
The part that gave me pause was the example that was used to parse local time omitted the +0000, which lead me to think the input string needed to have that removed, but it doesn't.
example:
var time = "2012-12-31T00:00:00+0000";
var x = moment(time); // Sun Dec 30 2012 19:00:00 GMT-0500
var y = moment(time,'YYYY-MM-DD'); //Mon Dec 31 2012 00:00:00 GMT-0500
You can ignore the browser's timezone completely by creating a new moment using moment.utc() instead of moment().
For example, if you are trying to work purely with a UTC date/time of the browser's current time but want to discard its timezone data, you can recreate the browser's current time into a UTC format using the following:
let nowWithTimezone = moment();
let nowInUtc = moment.utc(nowWithTimezone.format('MM/DD/YYYY HH:mm'), 'MM/DD/YYYY HH:mm');
Further documentation on moment.utc(): https://momentjs.com/docs/#/parsing/utc/
If you know for sure your input string is in the ISO-8601 format, you could just strip off the last 5 digits and use that in the Moment constructor.
var input = "2012-12-31T00:00:00+0000"
input = input.substring(0, input.length-5)
moment(input).toString()
> "Mon Dec 31 2012 00:00:00 GMT-0600"
There are valid reasons to do what the OP is asking for. The easiest way to do this with Moment is using its parseZone(date) method. No futzing around with string manipulation or multiple calls. It effectively parses the date string as though it were in the browser's local time zone.
This is difficult task to do with MomentJS, it will basically depend as well on your current timezone.
Documentation as well is vague for this specific task, the way I solved the issue on my side was by adding hours to the date before converting it to JSON format.
var dt = moment("Sun Sep 13 2015 00:00:00 GMT-0400", "ddd MMM DD YYYY HH:mm:ss GMT-0400", false);
var date = dt.add(2, 'hour').toJSON();
console.log(date); //2015-09-13T00:00:00.000Z
Momentjs default logic will format the given time with local timezone. To format original date, I wrote a function:
https://github.com/moment/moment/issues/2788#issuecomment-321950638
Use moment.parseZone to convert without taking into account the timezone.
const moment = require('moment')
const dateStr = '2020-07-21T10:00:00-09'
const date = moment.parseZone(dateStr)
console.log(date.format('MM-DD-YY HH:mm A')) // 07-21-20 10:00 AM
Try here link to docs
The best way is to use:
dt = moment("Wed Sep 16 2015 18:31:00 GMT-0400", "ddd MMM DD YYYY HH:mm:ss GMT-0400",true);
And to display convert again to desired timezone:
dt.utcOffset("-04:00").toString()
output > Wed Sep 16 2015 18:31:00 GMT-0400
var stdate=document.forms["myForm"]["from"].value;
var endate=document.forms["myForm"]["to"].value;
var fromDate = new Date(stdate);
var toDate = new Date(endate);
alert(fromDate);
Input:
from: 19-Mar-2014 03:13:50 PM
Output: (in IE & Firefox)
invalid Date
in Chrome broswer:
Wed Mar 19 2014 15:13:50 GMT-0400 (Eastern Daylight Time)
What date format should I use so that all browsers support? Or how should I handle it?
In most cases, new Date(datestring) is all you need. But you need to chose the format carefully.
Here's a nice compatibility table: http://dygraphs.com/date-formats.html
The short version is this:
Dates only? use YYYY/mm/DD; never use hyphens in this format
Need time, but local time is OK? use YYYY/mm/DD HH:MM:SS; again, no hyphens
OK to ignore IE<9? Consider using ISO8601 with whole seconds (YYYY-mm-DDTHH:MM:SSZ, or with time-zone)
Need a UTC time in IE8? You'll have to do something clever. Xotic's answer looks good here.
As documented on MSDN (IE) - the Date constructor supports either a timestamp or a Datestring in a Format supported by Date.parse. According to the docs this is either RFC2822 or ISO 8601. some examples: 2011-10-10 or Mon, 25 Dec 1995 13:30:00 GMT should be valid arguments.
I would use an ISO8601
formatted string in UTC as my input, but then manually
split
this string into parts and feed them into my
Date
constructor thus avoiding date parsing issues.
Example
var iso8601 = '2014-03-19T03:13:50.000Z',
parts = iso8601.slice(0, -1).split(/[\-T:]/),
dateObject;
parts[1] -= 1;
dateObject = new Date(Date.UTC.apply(undefined, parts));
console.log(dateObject.toUTCString());
Output
Wed, 19 Mar 2014 03:13:50 GMT
On jsFiddle