PHP में जैसे जावास्क्रिप्ट में वर्ष का सप्ताह प्राप्त करें


140

मुझे पीएचपी की तरह, वर्ष का मौजूदा सप्ताह का अंकुरण कैसे मिलेगा date('W')?

यह वर्ष का ISO-8601 सप्ताह होना चाहिए , सोमवार से शुरू होने वाले सप्ताह।


1
<a href=" javascript.about.com/library/blweekyear.htm "> <b> यहां </ b> </a> देखें, जो कि 'javascript वीक ऑफ ईयर' होने पर दिया गया पहला लिंक था।
पीट विल्सन

+1 लोल! यहीं से मुझे खुद से स्निपेट मिल गया, लेकिन मुझे वह स्रोत याद नहीं आ रहा था जैसा कि मुझे कुछ समय पहले मिला था।
टॉम चैंटलर

@ पता: वह कोड चालू सप्ताह के रूप में 22 हो जाता है। जबकि यह 21
PeeHaa

@Pete:: D Nopez एक सरल -1 चाल नहीं चलेगा: P कि ISO-8601 वीकम्बर नहीं मिलेगा। आईएसओ -8601 में एक सप्ताह सोमवार से शुरू होता है। पहला सप्ताह सप्ताह है जिसमें वर्ष का पहला गुरुवार है। en.wikipedia.org/wiki/ISO-8601 । पीएस मुझे नहीं था जो तुम्हें नीचा दिखाया
PeeHaa

जवाबों:


276

आप यहाँ जो चाहते हैं उसे पाने में सक्षम होना चाहिए: http://www.merlyn.demon.co.uk/js-date6.htm#YWD

एक ही साइट पर एक बेहतर लिंक है: सप्ताह के साथ काम करना

संपादित करें

यहाँ कुछ लिंक दिए गए लिंक पर आधारित है और जो डोमर द्वारा पोस्ट किए गए ईयरलर हैं। Http://www.merlyn.demon.co.uk/js-date6.htm#YWD पर परिणामों के खिलाफ इसका हल्का परीक्षण किया गया है । कृपया अच्छी तरह से परीक्षण करें, कोई गारंटी नहीं दी गई है।

2017 को संपादित करें

इस अवधि के दौरान तारीखों के साथ एक मुद्दा था कि दिन के उजाले की बचत देखी गई थी और उन वर्षों में जहां 1 जनवरी शुक्रवार था। सभी UTC विधियों का उपयोग करके फिक्स्ड। निम्नलिखित Moment.js के समान परिणाम देता है।

/* For a given date, get the ISO week number
 *
 * Based on information at:
 *
 *    http://www.merlyn.demon.co.uk/weekcalc.htm#WNR
 *
 * Algorithm is to find nearest thursday, it's year
 * is the year of the week number. Then get weeks
 * between that date and the first day of that year.
 *
 * Note that dates in one year can be weeks of previous
 * or next year, overlap is up to 3 days.
 *
 * e.g. 2014/12/29 is Monday in week  1 of 2015
 *      2012/1/1   is Sunday in week 52 of 2011
 */
function getWeekNumber(d) {
    // Copy date so don't modify original
    d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
    // Set to nearest Thursday: current date + 4 - current day number
    // Make Sunday's day number 7
    d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay()||7));
    // Get first day of year
    var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
    // Calculate full weeks to nearest Thursday
    var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7);
    // Return array of year and week number
    return [d.getUTCFullYear(), weekNo];
}

var result = getWeekNumber(new Date());
document.write('It\'s currently week ' + result[1] + ' of ' + result[0]);

"UTC" तिथि बनाते समय घंटे शून्य कर दिए जाते हैं।

न्यूनतम, प्रोटोटाइप संस्करण (केवल सप्ताह-संख्या रिटर्न):

Date.prototype.getWeekNumber = function(){
  var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
  var dayNum = d.getUTCDay() || 7;
  d.setUTCDate(d.getUTCDate() + 4 - dayNum);
  var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
  return Math.ceil((((d - yearStart) / 86400000) + 1)/7)
};

document.write('The current ISO week number is ' + new Date().getWeekNumber());

टेस्ट सेक्शन

इस खंड में, आप YYYY-MM-DD प्रारूप में किसी भी तारीख को दर्ज कर सकते हैं और जांच सकते हैं कि यह कोड एक ही सप्ताह का नंबर देता है जैसे मोमेंट.जेएस आईएसओ सप्ताह संख्या (2000 से 2050 तक 50 वर्षों में परीक्षण किया गया)।

Date.prototype.getWeekNumber = function(){
  var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
  var dayNum = d.getUTCDay() || 7;
  d.setUTCDate(d.getUTCDate() + 4 - dayNum);
  var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
  return Math.ceil((((d - yearStart) / 86400000) + 1)/7)
};

function checkWeek() {
  var s = document.getElementById('dString').value;
  var m = moment(s, 'YYYY-MM-DD');
  document.getElementById('momentWeek').value = m.format('W');
  document.getElementById('answerWeek').value = m.toDate().getWeekNumber();      
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

Enter date  YYYY-MM-DD: <input id="dString" value="2021-02-22">
<button onclick="checkWeek(this)">Check week number</button><br>
Moment: <input id="momentWeek" readonly><br>
Answer: <input id="answerWeek" readonly>


8
यह कोड 2 जनवरी 2011 को 2010 के 53 वें सप्ताह के रूप में गणना करता है जहां यह 52 वां होना चाहिए। यह मूल कोड में सही ढंग से काम करता है लेकिन आपके अनुकूलन में नहीं।
अलसादेयर

4
तुमने मेरी गांड बचा ली। धन्यवाद। यदि आप Open Source में योगदान करना चाहते हैं, तो मेरा सुझाव है कि आप jQuery UI विधि के लिए एक पैच बनाएँ: $ .datepicker.iso8601Week (तिथि) क्योंकि यह केवल सप्ताह भर में वापसी करता है, लेकिन कोई वर्ष नहीं।
क्रिश्चियन

18
आज, ४ जनवरी २०१६, मैंने देखा कि इसे जोड़ने की नौटंकी थी d.setMilliseconds(0)- यह एक ही तारीख के लिए अलग-अलग सप्ताह के नंबर दिखाती रही, इस पर निर्भर करते हुए कि मैंने नई तारीख () या नई तारीख ("1/4/2016") का उपयोग किया है। बस दूसरों के लिए एक सिर जो एक ही अनुभव हो सकता है।
याकूब लॉरिटजन

2
बशर्ते कोड आईएसओ 8601 का पालन नहीं करता है, यह एक से दूर है
एरिक ग्रेंज

2
उफ़ आप सही कह रहे हैं, मेरा टाइपो, '2015-12-30' माना जाता था जो काम करता है।
सहयोगी


26

जैसा कि ऊपर कहा गया है लेकिन एक वर्ग के बिना:

let now = new Date();
let onejan = new Date(now.getFullYear(), 0, 1);
week = Math.ceil( (((now - onejan) / 86400000) + onejan.getDay() + 1) / 7 );

4
एक जनवरी-तांग! * निंजा-रोल *
CodeManX

2
यह एकमात्र उत्तर है जो एक वर्ष के पहले सप्ताह के लिए सही सप्ताह संख्या प्राप्त करता है।
प्रसाद

(now.getTime() - onejan.getTime())समस्याओं के निर्माण से बचने के लिए ध्यान दें ।
स्वोक्स

4
आईएसओ 8601 के लिए प्रश्न पूछा गया था जो इस पर ध्यान नहीं देता है। प्रश्न का उत्तर के रूप में यह केवल गलत है
havlock

23

अकस्मात http://javascript.about.com/library/blweekyear.htm

Date.prototype.getWeek = function() {
    var onejan = new Date(this.getFullYear(),0,1);
    var millisecsInDay = 86400000;
    return Math.ceil((((this - onejan) /millisecsInDay) + onejan.getDay()+1)/7);
};

1
संक्षिप्त करें, लेकिन रविवार को सप्ताह के पहले दिन के रूप में मानते हैं, इसलिए रविवार 27 दिसंबर 2015 को सप्ताह के अंतिम दिन 52 के बजाय सप्ताह 53 का पहला दिन है।
रॉब जूल

3
मुझे लगता है कि जब से इसे प्रोटोटाइप में जोड़ा जा रहा है, तो यह वही होगा जो आप पहले दिन के रूप में रविवार को मानते हैं।
एड साइक्स

क्या इससे डेलाइट सेविंग टाइम के दिनों में समस्या नहीं होगी? मुझे लगता है कि यह गर्मियों के दौरान 1 बजे तक आगे नहीं बढ़ेगा।
हफथोर

इसके अलावा, यह तकनीकी रूप से सप्ताह को 0: 00: 00.001 तक आगे नहीं बढ़ाता है? Math.floor का उपयोग करने के लिए बेहतर है?
हफथोर

11

याकूब राइट की Date.format()लाइब्रेरी PHP के date()कार्य की शैली में प्रारूपण को लागू करती है और ISO-8601 सप्ताह की संख्या का समर्थन करती है:

new Date().format('W');

यह सिर्फ एक सप्ताह की संख्या के लिए थोड़ा अधिक हो सकता है, लेकिन यह PHP शैली प्रारूपण का समर्थन करता है और यदि आप इसमें से बहुत कुछ कर रहे हैं तो यह काफी उपयोगी है।


त्वरित हैक-एक साथ लिपियों के लिए अच्छा समाधान :)
स्टीन शूइट

6
getWeekOfYear: function(date) {
        var target = new Date(date.valueOf()),
            dayNumber = (date.getUTCDay() + 6) % 7,
            firstThursday;

        target.setUTCDate(target.getUTCDate() - dayNumber + 3);
        firstThursday = target.valueOf();
        target.setUTCMonth(0, 1);

        if (target.getUTCDay() !== 4) {
            target.setUTCMonth(0, 1 + ((4 - target.getUTCDay()) + 7) % 7);
        }

        return Math.ceil((firstThursday - target) /  (7 * 24 * 3600 * 1000)) + 1;
    }

निम्नलिखित कोड टाइमजोन-इंडिपेंडेंट (यूटीसी तारीखों का इस्तेमाल किया गया) और https://en.wikipedia.org/wiki/ISO_8601 के अनुसार काम करता है


4

मुझे ओरेकल के विनिर्देशन पर वर्णित जावा एसई के सिंपलडेटफार्मैट क्लास उपयोगी पाया गया : http://goo.gl/7MbCh5 । Google Apps स्क्रिप्ट में मेरे मामले में इस तरह काम किया:

function getWeekNumber() {
  var weekNum = parseInt(Utilities.formatDate(new Date(), "GMT", "w"));
  Logger.log(weekNum);
}

एक स्प्रेडशीट मैक्रो में उदाहरण के लिए आप फ़ाइल का वास्तविक समय क्षेत्र पुनः प्राप्त कर सकते हैं:

function getWeekNumber() {
  var weekNum = parseInt(Utilities.formatDate(new Date(), SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone(), "w"));
  Logger.log(weekNum);
}

4

यह Date.prototyp में "getWeek" विधि जोड़ता है जो वर्ष की शुरुआत से सप्ताह की संख्या देता है। तर्क यह परिभाषित करता है कि पहले विचार करने के लिए सप्ताह का कौन सा दिन है। यदि कोई तर्क पारित नहीं हुआ, तो पहले दिन को रविवार माना जाता है।

/**
 * Get week number in the year.
 * @param  {Integer} [weekStart=0]  First day of the week. 0-based. 0 for Sunday, 6 for Saturday.
 * @return {Integer}                0-based number of week.
 */
Date.prototype.getWeek = function(weekStart) {
    var januaryFirst = new Date(this.getFullYear(), 0, 1);
    if(weekStart !== undefined && (typeof weekStart !== 'number' || weekStart % 1 !== 0 || weekStart < 0 || weekStart > 6)) {
      throw new Error('Wrong argument. Must be an integer between 0 and 6.');
    }
    weekStart = weekStart || 0;
    return Math.floor((((this - januaryFirst) / 86400000) + januaryFirst.getDay() - weekStart) / 7);
};

1
2016 का पहला कैलेंडर सप्ताह 4 जनवरी को जर्मनी में शुरू होता है, लेकिन आपका कार्य 1 जनवरी से 0 से फिर से शुरू होता है। यह वर्ष के अंत में गलत संख्या भी देता है, उदाहरण के लिए 2018-11-31 (53 वें सप्ताह) के लिए 52, हालांकि यह 2019 का पहला कैलेंडर सप्ताह है : new Date(Date.UTC(2018,11, 31)).getWeek(1)+1(सोमवार जर्मनी में सप्ताह का पहला दिन है)।
कोडमैनएक्स

यही कारण है कि यह इरादा था, और मुझे विश्वास है कि, सबसे अधिक संभावना मामला है। अन्यथा 2016 के पहले 3 दिन बाहर गिर जाते। महीने के पहले दिनों को उस महीने के पहले सप्ताह को शामिल करने के लिए माना जाता है, चाहे कोई भी हो और कितने दिन हैं। यदि आपको अलग तरह से काम करने के लिए फ़ंक्शन की आवश्यकता है, तो आप इसे अपनी आवश्यकताओं के अनुसार ट्विक कर सकते हैं। इसी तरह, यदि एक सप्ताह दिए गए वर्ष और अगले वर्ष दोनों में आता है, तो इसे उस वर्ष का अंतिम सप्ताह कहा जा सकता है, साथ ही अगले वर्ष का पहला सप्ताह (वर्तमान तर्क के अनुसार)।
तिगरान

जानकारी के लिए धन्यवाद। मैंने RobG के समाधान का उपयोग करते हुए समाप्त किया, जो ISO8601 सप्ताह की तारीखों को सही ढंग से लागू करता है (दिसंबर में आखिरी दिन और जनवरी में पहले दिन 52, 53 या 1 सप्ताह के हो सकते हैं: en.m.wikipedia.org/wiki/ISO_weekddate
CodeManX

4

किसी भी दी गई तारीख का हफ्ता प्राप्त करें

function week(year,month,day) {
    function serial(days) { return 86400000*days; }
    function dateserial(year,month,day) { return (new Date(year,month-1,day).valueOf()); }
    function weekday(date) { return (new Date(date)).getDay()+1; }
    function yearserial(date) { return (new Date(date)).getFullYear(); }
    var date = year instanceof Date ? year.valueOf() : typeof year === "string" ? new Date(year).valueOf() : dateserial(year,month,day), 
        date2 = dateserial(yearserial(date - serial(weekday(date-serial(1))) + serial(4)),1,3);
    return ~~((date - date2 + serial(weekday(date2) + 5))/ serial(7));
}

उदाहरण

console.log(
    week(2016, 06, 11),//23
    week(2015, 9, 26),//39
    week(2016, 1, 1),//53
    week(2016, 1, 4),//1
    week(new Date(2016, 0, 4)),//1
    week("11 january 2016")//2
);

1
मैं इस पर विश्वास नहीं कर सकता, लेकिन यह फ़ंक्शन केवल एक ही है जो हर समय काम करता है! स्वीकार किए गए उत्तर को तब निभाया गया जब वह दिन के उजाले की बचत में चला गया, अन्य लोगों ने '0' को कुछ वर्षों में सप्ताह की संख्या के रूप में कहा। - और कुछ यूटीसी कार्यों पर भरोसा करते हैं जो कभी-कभी पिछले दिन वापस लौटते हैं इसलिए इसे सप्ताह '53' या '54' निर्दिष्ट करते हैं। दुर्भाग्य से मुझे रविवार को शुरू करने के लिए सप्ताह की आवश्यकता है और इस कोड को समझना बहुत कठिन है ...
मेलिसा ज़चरियाडिस

@MelissaZachariadis ने कहा I need the week to begin on a Sunday; मुझे लगता है कि केवल कार्यदिवस में परिवर्तन की आवश्यकता है कार्यदिवस () के .getDay()+1परिवर्तन को.getDay()
राफा

4

नीचे दिया गया कोड सही ISO 8601 सप्ताह संख्या की गणना करता है। यह date("W")हर हफ्ते 1/1/1970 और 1/1/2100 के बीच PHP के मेल खाता है ।

/**
 * Get the ISO week date week number
 */
Date.prototype.getWeek = function () {
  // Create a copy of this date object
  var target = new Date(this.valueOf());

  // ISO week date weeks start on Monday, so correct the day number
  var dayNr = (this.getDay() + 6) % 7;

  // ISO 8601 states that week 1 is the week with the first Thursday of that year
  // Set the target date to the Thursday in the target week
  target.setDate(target.getDate() - dayNr + 3);

  // Store the millisecond value of the target date
  var firstThursday = target.valueOf();

  // Set the target to the first Thursday of the year
  // First, set the target to January 1st
  target.setMonth(0, 1);

  // Not a Thursday? Correct the date to the next Thursday
  if (target.getDay() !== 4) {
    target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7);
  }

  // The week number is the number of weeks between the first Thursday of the year
  // and the Thursday in the target week (604800000 = 7 * 24 * 3600 * 1000)
  return 1 + Math.ceil((firstThursday - target) / 604800000);
}

स्रोत: टैको वैन डेन ब्रोक


यदि आप प्रोटोटाइप का विस्तार नहीं कर रहे हैं, तो यहां एक फ़ंक्शन है:

function getWeek(date) {
  if (!(date instanceof Date)) date = new Date();

  // ISO week date weeks start on Monday, so correct the day number
  var nDay = (date.getDay() + 6) % 7;

  // ISO 8601 states that week 1 is the week with the first Thursday of that year
  // Set the target date to the Thursday in the target week
  date.setDate(date.getDate() - nDay + 3);

  // Store the millisecond value of the target date
  var n1stThursday = date.valueOf();

  // Set the target to the first Thursday of the year
  // First, set the target to January 1st
  date.setMonth(0, 1);

  // Not a Thursday? Correct the date to the next Thursday
  if (date.getDay() !== 4) {
    date.setMonth(0, 1 + ((4 - date.getDay()) + 7) % 7);
  }

  // The week number is the number of weeks between the first Thursday of the year
  // and the Thursday in the target week (604800000 = 7 * 24 * 3600 * 1000)
  return 1 + Math.ceil((n1stThursday - date) / 604800000);
}

नमूना उपयोग:

getWeek(); // Returns 37 (or whatever the current week is)
getWeek(new Date('Jan 2, 2011')); // Returns 52
getWeek(new Date('Jan 1, 2016')); // Returns 53
getWeek(new Date('Jan 4, 2016')); // Returns 1

मुझे यह फ़ंक्शन पसंद है, लेकिन मेरे पास एक सवाल है; अगर मैं इसे रविवार को वापस लाना चाहता हूं तो मुझे क्या करना चाहिए? मुझे कुछ पता नहीं है कि +6 ) % 7भाग क्या करता है। स्क्रब से धन्यवाद!
नोबिशप्रो

1
@Bydydead आईएसओ सप्ताह सोमवार से शुरू होता है, लेकिन जावास्क्रिप्ट getDay()रविवार को शुरू होता है, इसलिए यदि आप इसे रविवार को शुरू करना चाहते हैं, तो आप सुधार को हटा सकते हैं:var nDay = date.getDay();
thdoan

मैंने सप्ताह संख्या प्राप्त करने के लिए 8 अलग-अलग जेएस कार्यान्वयनों की कोशिश की है। यह एकमात्र कार्य है जो काम करता है, लेकिन केवल अगर मैं सभी गेटर्स को बदल देता हूं और getUTC .. और setUTC पर बस जाता हूं .. तो पता नहीं क्यों। मैं इसके साथ परीक्षण कर रहा था: 2017-07-17T00: 00: 00.000Z (सप्ताह 29) 2017-07-23T23: 59: 59.000Z (सप्ताह 29) 2021-01-04T00: 00: 00.000Z (सप्ताह 1)
साइको brm


2

कोड स्निपेट जो मेरे लिए बहुत अच्छा काम करता है वह यह है:

var yearStart = +new Date(d.getFullYear(), 0, 1);
var today = +new Date(d.getFullYear(),d.getMonth(),d.getDate());
var dayOfYear = ((today - yearStart + 1) / 86400000);
return Math.ceil(dayOfYear / 7).toString();

नोट:
dमेरी तिथि है जिसके लिए मुझे वर्तमान सप्ताह संख्या चाहिए। धर्मान्तरित संख्या में तिथियां (टाइपप्रति के साथ काम)।
+


1

यहां जावास्क्रिप्ट में सप्ताह की संख्या की गणना के लिए मेरा कार्यान्वयन है। गर्मियों और सर्दियों के समय के साथ-साथ ऑफसेट के लिए भी सही। मैंने इस लेख से सप्ताह की परिभाषा का उपयोग किया: आईएसओ 8601

सप्ताह रविवार से रविवार तक होते हैं, और वर्ष के पहले सप्ताह में 4 जी हमेशा रहता है।

// add get week prototype functions
// weeks always start from monday to sunday
// january 4th is always in the first week of the year
Date.prototype.getWeek = function () {
    year = this.getFullYear();
    var currentDotw = this.getWeekDay();
    if (this.getMonth() == 11 && this.getDate() - currentDotw > 28) {
        // if true, the week is part of next year 
        return this.getWeekForYear(year + 1);
    }
    if (this.getMonth() == 0 && this.getDate() + 6 - currentDotw < 4) {
        // if true, the week is part of previous year
        return this.getWeekForYear(year - 1);
    }
    return this.getWeekForYear(year);
}

// returns a zero based day, where monday = 0
// all weeks start with monday
Date.prototype.getWeekDay = function () {
    return  (this.getDay() + 6) % 7;
}

// corrected for summer/winter time
Date.prototype.getWeekForYear = function (year) {
    var currentDotw = this.getWeekDay();
    var fourjan = new Date(year, 0, 4);
    var firstDotw = fourjan.getWeekDay();
    var dayTotal = this.getDaysDifferenceCorrected(fourjan) // the difference in days between the two dates.
    // correct for the days of the week
    dayTotal += firstDotw; // the difference between the current date and the first monday of the first week, 
    dayTotal -= currentDotw; // the difference between the first monday and the current week's monday
    // day total should be a multiple of 7 now
    var weeknumber = dayTotal / 7 + 1; // add one since it gives a zero based week number.
    return weeknumber;
}

// corrected for timezones and offset
Date.prototype.getDaysDifferenceCorrected = function (other) {
    var millisecondsDifference = (this - other);
    // correct for offset difference. offsets are in minutes, the difference is in milliseconds
    millisecondsDifference += (other.getTimezoneOffset()- this.getTimezoneOffset()) * 60000;
    // return day total. 1 day is 86400000 milliseconds, floor the value to return only full days
    return Math.floor(millisecondsDifference / 86400000);
}

परीक्षण के लिए मैंने Qunit में निम्नलिखित जावास्क्रिप्ट परीक्षणों का उपयोग किया

var runweekcompare = function(result, expected) {
    equal(result, expected,'Week nr expected value: ' + expected + ' Actual value: ' + result);
}

test('first week number test', function () {
    expect(5);
    var temp = new Date(2016, 0, 4); // is the monday of the first week of the year
    runweekcompare(temp.getWeek(), 1);
    var temp = new Date(2016, 0, 4, 23, 50); // is the monday of the first week of the year
    runweekcompare(temp.getWeek(), 1);
    var temp = new Date(2016, 0, 10, 23, 50); // is the sunday of the first week of the year
    runweekcompare(temp.getWeek(), 1);
    var temp = new Date(2016, 0, 11, 23, 50); // is the second week of the year
    runweekcompare(temp.getWeek(), 2);
    var temp = new Date(2016, 1, 29, 23, 50); // is the 9th week of the year
    runweekcompare(temp.getWeek(), 9);
});

test('first day is part of last years last week', function () {
    expect(2);
    var temp = new Date(2016, 0, 1, 23, 50); // is the first last week of the previous year
    runweekcompare(temp.getWeek(), 53);
    var temp = new Date(2011, 0, 2, 23, 50); // is the first last week of the previous year
    runweekcompare(temp.getWeek(), 52);
});

test('last  day is part of next years first week', function () {
    var temp = new Date(2013, 11, 30); // is part of the first week of 2014
    runweekcompare(temp.getWeek(), 1);
});

test('summer winter time change', function () {
    expect(2);
    var temp = new Date(2000, 2, 26); 
    runweekcompare(temp.getWeek(), 12);
    var temp = new Date(2000, 2, 27); 
    runweekcompare(temp.getWeek(), 13);
});

test('full 20 year test', function () {
    //expect(20 * 12 * 28 * 2);
    for (i = 2000; i < 2020; i++) {
        for (month = 0; month < 12; month++) {
            for (day = 1; day < 29 ; day++) {
                var temp = new Date(i, month, day);
                var expectedweek = temp.getWeek();
                var temp2 = new Date(i, month, day, 23, 50);
                var resultweek = temp.getWeek();
                equal(expectedweek, Math.round(expectedweek), 'week number whole number expected ' + Math.round(expectedweek) + ' resulted week nr ' + expectedweek);
                equal(resultweek, expectedweek, 'Week nr expected value: ' + expectedweek + ' Actual value: ' + resultweek + ' for year ' + i + ' month ' + month + ' day ' + day);
            }
        }
    }
});

0

इस हफ्ते नंबर एक ** में वास्तविक दर्द रहा है। नेट के चारों ओर अधिकांश स्क्रिप्ट मेरे लिए काम नहीं करती थी। उन्होंने ज्यादातर समय काम किया लेकिन वे सभी किसी न किसी बिंदु पर टूट गए, खासकर जब साल बदल गया और साल का आखिरी सप्ताह अचानक अगले साल का पहला सप्ताह था आदि। यहां तक ​​कि एंगुलर की तारीख फिल्टर ने गलत डेटा दिखाया (यह अगले साल का पहला सप्ताह था, कोणीय दिया सप्ताह 53)।

नोट: उदाहरण यूरोपीय सप्ताह (सोम पहले) के साथ काम करने के लिए डिज़ाइन किए गए हैं!

getWeek ()

Date.prototype.getWeek = function(){

    // current week's Thursday
    var curWeek = new Date(this.getTime());
        curWeek.setDay(4);

    // Get year's first week's Thursday
    var firstWeek = new Date(curWeek.getFullYear(), 0, 4);
        firstWeek.setDay(4);

    return (curWeek.getDayIndex() - firstWeek.getDayIndex()) / 7 + 1;
};

setDay ()

/**
* Make a setDay() prototype for Date
* Sets week day for the date
*/
Date.prototype.setDay = function(day){

    // Get day and make Sunday to 7
    var weekDay = this.getDay() || 7;
    var distance = day - weekDay;
    this.setDate(this.getDate() + distance);

    return this;
}

getDayIndex ()

/*
* Returns index of given date (from Jan 1st)
*/

Date.prototype.getDayIndex = function(){
    var start = new Date(this.getFullYear(), 0, 0);
    var diff = this - start;
    var oneDay = 86400000;

    return Math.floor(diff / oneDay);
};

मैंने इसका परीक्षण किया है और ऐसा लगता है कि यह बहुत अच्छा काम कर रहा है लेकिन अगर आपको इसमें कोई खामी नजर आती है तो कृपया मुझे बताएं।


0

मैंने हफ़्तेभर का आईएसओ प्राप्त करने के लिए सबसे छोटा कोड प्राप्त करने की बहुत कोशिश की।

Date.prototype.getWeek=function(){
    var date=new Date(this);
    date.setHours(0,0,0,0);
    return Math.round(((date.setDate(this.getDate()+2-(this.getDay()||7))-date.setMonth(0,4))/8.64e7+3+(date.getDay()||7))/7)+"/"+date.getFullYear();}

dateमूल को बदलने से बचने के लिए चर आवश्यक है this। मैंने कोड लंबाई को बचाने के लिए setDate()और setMonth()के साथ वापसी के मूल्यों का getTime()उपयोग किया और मैंने एकल तत्वों के गुणन या पांच शून्य के साथ एक संख्या के बजाय एक दिन के मिलीसेकंड के लिए एक एक्सपोनेंटियल संख्या का उपयोग किया। thisमिलीसेकंड की तारीख या संख्या है, रिटर्न मान String"49/2017" है।



0

एक अन्य पुस्तकालय-आधारित विकल्प: उपयोग d3-time-format:

const formatter = d3.timeFormat('%U');
const weekNum = formatter(new Date());

0

Angular2 + DatePipe के लिए सबसे छोटा समाधान , ISO-8601 के लिए समायोजित:

import {DatePipe} from "@angular/common";

public rightWeekNum: number = 0;
  
constructor(private datePipe: DatePipe) { }
    
calcWeekOfTheYear(dateInput: Date) {
  let falseWeekNum = parseInt(this.datePipe.transform(dateInput, 'ww'));
  this.rightWeekNum = falseWeekNum ? falseWeekNum : falseWeekNum-1;
}

-1
now = new Date();
today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
firstOfYear = new Date(now.getFullYear(), 0, 1);
numOfWeek = Math.ceil((((today - firstOfYear) / 86400000)-1)/7);
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.