Need help in date using moment js ( 2015-06-02T00:00:00Z) - javascript

I am trying to fetch the video data from YouTube API.
This is my request:
https://www.googleapis.com/youtube/v3/search?key={{YOUKEY}}&channelId={{CHANNELID}}&part=snippet,id&order=date&maxResults=50&publishedAfter=2014-09-21T00:00:00Z&publishedBefore=2014-09-22T02:00:00Z
As you can see in this, the publishedAfter is in this way
2014-09-21T00:00:00Z with time set to zero. I tried using moment, but couldn't get the exact format.

the default moment.format() Date is displayed like you ask, however you could force it by using a format string like
moment(string).format('YYYY-MM-DDThh:mm:ss[Z]');
explanation
YYYY year in digits
MM month in digits (base 1)
DD day of year in digits
T a random letter
hh:mm:ss hour:minutes:seconds
[Z] escape sequence for printing the letter Z
the letter Z in the format string in fact prints the time zone, as seen in the docs.
the exact solution is
var formatString = 'YYYY-MM-DDT[00:00:00Z]';
moment(string).format(formatString);
fiddle

Related

Convert ISO-8601 string date to ORACLE timestamp with timezone datatype [duplicate]

I tried to convert a time-stamp ("1985-02-07T00:00:00.000Z") to a date and I failed to succeed in my several different attempts.
Below is the query I have tried:
select to_date('1985-02-07T00:00:00.000Z', 'YYYY-MM-DDTHH24:MI:SS.fffZ')
from dual;
Your suggestions are greatly appreciated.
to_date converts the input to a DATE type which does not support fractional seconds. To use fractional seconds you need to use a TIMESTAMP type which is created when using to_timestamp
pst's comment about the ff3 modifier is also correct.
"Constant" values in the format mask need to be enclosed in double quote
So the final statement is:
select to_timestamp('1985-02-07T00:00:00.000Z', 'YYYY-MM-DD"T"HH24:MI:SS.ff3"Z"')
from dual;
SQL> select cast(to_timestamp('1985-02-07T00:00:00.000Z', 'yyyy-mm-dd"T"hh24:mi:ss.ff3"Z"') as date)
2 from dual
3 /
CAST(TO_TIMESTAMP('
-------------------
07-02-1985 00:00:00
1 row selected.
Some rules to follow:
Literals must be double-quoted: MM expects a month number, "MM" expects a double-M.
The format for fractional seconds is FF, not F or FFF. You specify the number of digits with a trailing integer, e.g. FF3.
But dates cannot hold fractional seconds anyway so you cannot use FF3 in this context.
This works:
SELECT TO_DATE('1985-02-07T00:00:00', 'YYYY-MM-DD"T"HH24:MI:SS')
FROM dual;
I don't know if there's a way to ignore fractional seconds in TO_DATE() so I've used string manipulation functions to strip them out:
SELECT TO_DATE(SUBSTR('1985-02-07T00:00:00.000Z', 1, 19), 'YYYY-MM-DD"T"HH24:MI:SS')
FROM dual;
SELECT to_timestamp_tz('2012-08-08T09:06:14.000-07:00','YYYY-MM-DD"T"HH24:MI:SS.FF3TZR')
FROM dual;
External table DDL,
extract_date char(29) DATE_FORMAT timestamp WITH TIMEZONE mask 'YYYY-MM-DD"T"HH24:MI:SS.FF3TZR'
if u want to get the string in datetime format then try this....
select to_char(TO_DATE('2012-06-26T00:00:00.809Z', 'YYYY-MM-DD"T"HH24:MI:SS".""ZZZZ"'),'yyyy-MM-dd hh:mm:ss PM') as EVENT_DATE from dual
EVENT_DATE
-----------------------
2012-06-26 12:06:00 AM
only for date simply use...
select TO_DATE('2012-01-06T00:00:00.809Z', 'YYYY-MM-DD"T"HH24:MI:SS".""ZZZZ"') from dual

Moment js: format date without zero padding

How can I format a date with momentjs without leading zeros? E.g. 2018-6-29 instead of 2018-06-29.
If the number is smaller than 10 it should not add 0 before it.
According to the docs (https://momentjs.com/docs/#/parsing/string-format/) if you use M or D instead of MM or DD in the format() function you will get the date without the 0.
moment().format("YYYY-M-DD") is what you are after.
if you also want to exclude the 0 from single digit days you can use:
moment().format("YYYY-M-D")
(fiddle here: http://jsfiddle.net/rLjQx/69671/)
You should use the format string YYYY-M-D.
Consult the documentation of how to format moments. https://momentjs.com/docs/#/displaying/format/

CSS JS date formatting issue

I have an unusual date formatting issue that, perhaps, someone else has encountered.
I have an MVC application with textboxes with #class = datepicker.
I have a global formatting string (dd-MMM-yyyy)
There's a bit of JS:
$(function () {
$('.datepicker').datepicker({
format: 'dd-MMM-yyyy',
autoclose: true
});
});
Issue: When I click on the textbox and select a date, I get something like the following:
01-JanuaryJan-2017
I assumed that, maybe, i had some formatting somewhere like dd-MMMMMMM-YYYY, but I do not. I'm at a loss.
I've inherited this application and am not extremely well-versed in web development, but this seems like an issue that I should be able to figure out.
I realize i don't have a lot of details here, but I'm also only looking for generic/high-level solutions like:
Check in this general area, or look for this, etc.
You have a wrong format. Use the right one from the following (as you wish):
format: 'dd-MM-yy'
which will display the date like this 03-January-2017
OR
format: 'dd-M-yy'
which will display the date like this 03-Jan-2017
Here is the (incomplete) list of valid values for the dateFormat property from the jQueryUI DatePicker Docs:
The format can be combinations of the following:
d - day of month (no leading zero)
dd - day of month (two digit)
o - day of the year (no leading zeros)
oo - day of the year (three digit)
D - day name short
DD - day name long
m - month of year (no leading zero)
mm - month of year (two digit)
M - month name short
MM - month name long
y - year (two digit)
yy - year (four digit)
# - Unix timestamp (ms since 01/01/1970)
! - Windows ticks (100ns since 01/01/0001)
'...' - literal text
'' - single quote
anything else - literal text
So, in your case, you are combining MM, which is the full month name with M which is the short month name, resulting in JanuaryJan.

How to understand zone by MOMENT.js

I have problem with String > Format of timezone.
I have string: " 2015-02-10 00:00:00,3,UTC "
And try to format it in moment:
moment('2015-02-10 00:00:00,3,UTC', 'YYYY-MM-DD HH:mm:ss, ?, ?')
What should I insert instead of "?"
It's not possible to format that kind of string into moment because a lone 3 does not designate the timezone offset in any standard format.
You need to change the 3 into +0030.
This should work:
var date = '2015-02-10 00:00:00,3,UTC'
.replace(/,(\d\d),/,',+$100,') // for double digit cases (11 turns to +1100)
.replace(/,(\d),/,',+0$100,'); // single digit cases (3 to +0300)
And then
moment(date, 'YYYY-MM-DD HH:mm:ss,ZZ')
I'm not sure what the UTC part is about since +0300 is clearly not UTC. I guess it's just saying that the the 3 hour offset is relative to UTC?

JS Date: Add 0 in front of every single day or month

I have this date parsed from an api as a string:
DD-MM-YYYY but sometimes the date is DD-M-YYYY or even D-M-YYYY.
For example:
4-1-2013
or
10-10-2013
or 7-4-2013
The year is always 4 digits but days or months sometimes get one digit. How can I manually (with JS) add 0 in front of a every single digit ?
I am using moment.js for some calculations thus I am remove the '-' using
date.replace("-", "")
to get a whole number (eg. 4-1-2013 = 412013) so I can use it with moment.js but if its a single digit, it all gets messed up.
You can normalise your strings first like this:
date = date.replace(/\b(\d{1})\b/g, '0$1');
which will take any "word" that is just a single digit and prepend a zero.
Note that to globally replace every - you must use the regexp version of .replace - the string version you've used only replaces the first occurrence, therefore:
date = date.replace(/\b(\d{1})\b/g, '0$1').replace(/-/g, '');
will do everything you require.
Moment.js supports formats with dashes, so you don't even need to do any string handling.
moment('4-1-2013', 'MM-DD-YYYY').format("MMDDYYYY"); // 04012013
moment('10-10-2013', 'MM-DD-YYYY').format("MMDDYYYY"); // 10102013
If the date variable is in a String format(for example from input[type=date]) then you need to split the data component into single items.
date = date.split('-');
then check both the day's and the month's length
day = date[0].length==1 ? "0"+date[0] : date[0];
month = date[1].length==1 ? "0"+date[1] : date[1];
then get them back together into a format that you desire
date = ""+day+month+date[2];
Working example: http://jsfiddle.net/dzXPE/
var day = new Date();
day = day.getDay();
if(day.toString().length <= 1) {
day = '0' + day;
}
You can use the same for month. I'm not entirely sure you need to convert to string but it wouldn't hurt.

Categories

Resources